-
Notifications
You must be signed in to change notification settings - Fork 11
feat: use high level zapi dsl #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
a48baa2
887a03e
f1f1097
6e542df
10db6b7
cab9a5f
b273436
b5a199f
273fcf1
c8962d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| const std = @import("std"); | ||
| const napi = @import("zapi:napi"); | ||
| const napi = @import("zapi").napi; | ||
| const js = @import("zapi").js; | ||
| const active_preset = @import("preset").active_preset; | ||
| const c = @import("config"); | ||
| const BeaconConfig = @import("config").BeaconConfig; | ||
|
|
@@ -41,29 +42,31 @@ pub const State = struct { | |
|
|
||
| pub var state: State = .{}; | ||
|
|
||
| pub fn Config_set(env: napi.Env, cb: napi.CallbackInfo(2)) !napi.Value { | ||
| /// JS: config.set(chainConfigObj, genesisValidatorsRoot) | ||
| pub fn set(obj: js.Value, genesis_root: js.Uint8Array) !void { | ||
| if (!state.initialized) { | ||
| return error.ConfigNotInitialized; | ||
| } | ||
|
|
||
| const obj = try cb.arg(0).coerceToObject(); | ||
| const chain_config = try chainConfigFromObject(env, obj); | ||
| const genesis_validators_root_info = try cb.arg(1).getTypedarrayInfo(); | ||
| if (genesis_validators_root_info.data.len != 32) { | ||
| // Drop to low-level for the complex object parsing | ||
| const e = js.env(); | ||
| const raw_obj = try obj.toValue().coerceToObject(); | ||
| const chain_config = try chainConfigFromObject(e, raw_obj); | ||
|
nazarhussain marked this conversation as resolved.
Outdated
|
||
|
|
||
| const root_slice = try genesis_root.toSlice(); | ||
| if (root_slice.len != 32) { | ||
| return error.InvalidGenesisValidatorsRootLength; | ||
| } | ||
|
|
||
| state.config = BeaconConfig.init( | ||
| chain_config, | ||
| genesis_validators_root_info.data[0..32].*, | ||
| root_slice[0..32].*, | ||
| ); | ||
|
|
||
| state.initialized = true; | ||
|
|
||
| return env.getUndefined(); | ||
| } | ||
|
|
||
| pub fn chainConfigFromObject(env: napi.Env, obj: napi.Value) !ChainConfig { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the |
||
| fn chainConfigFromObject(env: napi.Env, obj: napi.Value) !ChainConfig { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function References
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of scope from this PR. |
||
| var chain_config: ChainConfig = undefined; | ||
| inline for (std.meta.fields(ChainConfig)) |field| { | ||
| const field_value = obj.getNamedProperty(field.name) catch |err| { | ||
|
|
@@ -143,15 +146,3 @@ pub fn chainConfigFromObject(env: napi.Env, obj: napi.Value) !ChainConfig { | |
| } | ||
| return chain_config; | ||
| } | ||
|
|
||
| pub fn register(env: napi.Env, exports: napi.Value) !void { | ||
| const config_obj = try env.createObject(); | ||
| try config_obj.setNamedProperty("set", try env.createFunction( | ||
| "set", | ||
| 2, | ||
| Config_set, | ||
| null, | ||
| )); | ||
|
|
||
| try exports.setNamedProperty("config", config_obj); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| const std = @import("std"); | ||
| const napi = @import("zapi:napi"); | ||
| const js = @import("zapi").js; | ||
| const Node = @import("persistent_merkle_tree").Node; | ||
|
|
||
| /// Pool uses page allocator for internal allocations. | ||
|
|
@@ -27,27 +27,16 @@ pub const State = struct { | |
|
|
||
| pub var state: State = .{}; | ||
|
|
||
| pub fn Pool_ensureCapacity(env: napi.Env, cb: napi.CallbackInfo(1)) !napi.Value { | ||
| /// JS: pool.ensureCapacity(newSize) | ||
| pub fn ensureCapacity(new_size: js.Number) !void { | ||
| if (!state.initialized) { | ||
| return error.PoolNotInitialized; | ||
| } | ||
|
|
||
| const requested = new_size.assertU32(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see that the DSL contract enforces that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type This is same as |
||
| const old_size = state.pool.nodes.capacity; | ||
| const new_size = try cb.arg(0).getValueUint32(); | ||
| if (new_size <= old_size) { | ||
| return env.getUndefined(); | ||
| if (requested <= old_size) { | ||
| return; | ||
| } | ||
| try state.pool.preheat(@intCast(new_size - state.pool.nodes.capacity)); | ||
| return env.getUndefined(); | ||
| } | ||
|
|
||
| pub fn register(env: napi.Env, exports: napi.Value) !void { | ||
| const pool_obj = try env.createObject(); | ||
| try pool_obj.setNamedProperty("ensureCapacity", try env.createFunction( | ||
| "ensureCapacity", | ||
| 1, | ||
| Pool_ensureCapacity, | ||
| null, | ||
| )); | ||
| try exports.setNamedProperty("pool", pool_obj); | ||
| try state.pool.preheat(@intCast(requested - state.pool.nodes.capacity)); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function lacks assertions. The repository style guide requires that all function arguments, return values, pre/postconditions, and invariants be asserted. Furthermore, the assertion density must average a minimum of two assertions per function.
References
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arguments are asserted inside the functions.