Soroban: Support for Constructors#1675
Soroban: Support for Constructors#1675tareknaser wants to merge 5 commits intohyperledger-solang:mainfrom
Conversation
…tructors Signed-off-by: Tarek <tareknaser360@gmail.com>
…n logic Signed-off-by: Tarek <tareknaser360@gmail.com>
Signed-off-by: Tarek <tareknaser360@gmail.com>
Signed-off-by: Tarek <tareknaser360@gmail.com>
tareknaser
left a comment
There was a problem hiding this comment.
So adding support for constructors with custom arguments wasn't straightforward. I have two approaches in mind that I thought of, which I’ll explain below.
Let me know what you think, or if there’s a better, more straightforward approach I might be missing.
| // call the user defined constructor (if any) | ||
| if let Some(cfg_no) = constructor_cfg_no { | ||
| let constructor = binary.functions[cfg_no]; | ||
| let constructor_name = constructor.get_name().to_str().unwrap(); | ||
| binary | ||
| .builder | ||
| .build_call(constructor, &[], constructor_name) | ||
| .unwrap(); | ||
| } |
There was a problem hiding this comment.
We can set cfg.params to the parameters of the constructor FunctionValue's params. This will likely include a mapping that looks something like:
let mut params = vec![];
for param in constructor.get_params() {
let param = match param.get_type() {
BasicTypeEnum::IntType(int_type) => {
let ty = match int_type.get_bit_width() {
1 => ast::Type::Bool,
32 => ast::Type::Uint(32),
64 => ast::Type::Uint(64),
_ => unreachable!(),
};
ast::Parameter::new_default(ty)
}
_ => unreachable!(),
};
params.push(param);
}
cfg.params = sync::Arc::new(params);This won’t work because we still need to pass the arguments to build_call() (on line 296), which, of course, we don’t know.
|
|
||
| Self::emit_initializer(&mut binary, ns); | ||
| Self::emit_initializer(&mut binary, ns, contract.constructors(&ns).first()); | ||
|
|
There was a problem hiding this comment.
Another approach I have in mind is to get the constructor ControlFlowGraph itself and prepend it with a call to storage_initializer, but this won't work either because the contract is immutable.
We could clone it and re-export it, but I’m not sure if that’s the best approach.
Signed-off-by: Tarek <tareknaser360@gmail.com>
87ab13a to
4f2d6de
Compare
seanyoung
left a comment
There was a problem hiding this comment.
Looks good to me, really needs @salaheldinsoliman approval for merging (as he looks after the Soroban target)
This PR adds support for user-defined constructors in Soroban, specifically for constructors with no arguments. Note that constructors with arguments are not yet supported (see comments for details).
Part of #1672
Other changes include:
tests/soroban.rsto allow providing constructors when registering contracts on the chain.tests/soroban_testcases/constructor.rs.