-
Notifications
You must be signed in to change notification settings - Fork 19
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
Write literal values to memory at compile-time #605
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #605 +/- ##
==========================================
- Coverage 85.65% 85.61% -0.05%
==========================================
Files 46 46
Lines 24554 24590 +36
Branches 24554 24590 +36
==========================================
+ Hits 21032 21052 +20
- Misses 1672 1681 +9
- Partials 1850 1857 +7 ☔ View full report in Codecov by Sentry. |
for expr in list.iter() { | ||
match expr.match_literal_value() { | ||
Some(Value::Int(i)) => literal_data.extend_from_slice(&i.to_le_bytes()), | ||
Some(Value::UInt(u)) => literal_data.extend_from_slice(&u.to_le_bytes()), | ||
Some(Value::Bool(b)) => literal_data.extend_from_slice(&[*b as u8]), | ||
Some(Value::Sequence(SequenceData::String(string_data))) => { | ||
let (offset, len) = generator.add_clarity_string_literal(string_data)?; | ||
literal_data.extend_from_slice(&offset.to_le_bytes()); | ||
literal_data.extend_from_slice(&len.to_le_bytes()); | ||
} | ||
Some(Value::Sequence(SequenceData::Buffer(buffer))) => { | ||
let (offset, len) = generator | ||
.add_literal(&Value::Sequence(SequenceData::Buffer(buffer.clone())))?; | ||
literal_data.extend_from_slice(&offset.to_le_bytes()); | ||
literal_data.extend_from_slice(&len.to_le_bytes()); | ||
} | ||
Some(Value::Principal(PrincipalData::Standard(standard))) => { | ||
let (offset, len) = generator.add_literal(&Value::Principal( | ||
PrincipalData::Standard(standard.clone()), | ||
))?; | ||
literal_data.extend_from_slice(&offset.to_le_bytes()); | ||
literal_data.extend_from_slice(&len.to_le_bytes()); | ||
} | ||
Some(Value::Principal(PrincipalData::Contract(contract))) => { | ||
let (offset, len) = generator.add_literal(&Value::Principal( | ||
PrincipalData::Contract(contract.clone()), | ||
))?; | ||
literal_data.extend_from_slice(&offset.to_le_bytes()); | ||
literal_data.extend_from_slice(&len.to_le_bytes()); | ||
} | ||
_ => { | ||
return Err(GeneratorError::TypeError( | ||
"Unsupported literal type in list".to_owned(), | ||
)) | ||
} | ||
} | ||
} |
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.
I think we should use write_to_wasm()
here. The function already exists and is heavily tested.
matches!( | ||
expr.match_literal_value(), | ||
Some(Value::Int(_)) | ||
| Some(Value::UInt(_)) | ||
| Some(Value::Bool(_)) | ||
| Some(Value::Sequence(SequenceData::String(_))) | ||
| Some(Value::Sequence(SequenceData::Buffer(_))) | ||
| Some(Value::Principal(_)) | ||
) |
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 is a good start, but what about (list {a: 1} {a: 2})
or (list (list 1) (list 2))
?
I think the easiest thing to do here would be to create a function that checks if all the leaves in a SymbolicExpression
tree are LiteralValue
and check if all elements in list
have this property.
This PR introduces a feature where lists containing only literal elements, such as
(list 1 2 3)
, are no longer constructed at runtime. Instead, the compiler now identifies these lists and computes their memory representation during compile-time.Closes: #587