Skip to content

Commit

Permalink
feat: MORE builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed Oct 12, 2024
1 parent 3723373 commit 72a91d4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
6 changes: 6 additions & 0 deletions crates/uplc/src/machine/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub enum RuntimeError<'a> {
ByteStringConsNotAByte(&'a Integer),
#[error(transparent)]
Secp256k1(#[from] secp256k1::Error),
#[error(transparent)]
DecodeUtf8(#[from] std::str::Utf8Error),
}

impl<'a> MachineError<'a> {
Expand Down Expand Up @@ -123,4 +125,8 @@ impl<'a> MachineError<'a> {
pub fn secp256k1(error: secp256k1::Error) -> Self {
MachineError::runtime(RuntimeError::Secp256k1(error))
}

pub fn decode_utf8(error: std::str::Utf8Error) -> Self {
MachineError::runtime(RuntimeError::DecodeUtf8(error))
}
}
66 changes: 60 additions & 6 deletions crates/uplc/src/machine/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::array::TryFromSliceError;

use bumpalo::{
collections::{CollectIn, Vec as BumpVec},
collections::{CollectIn, String as BumpString, Vec as BumpVec},
Bump,
};
use rug::{Assign, Integer};
Expand Down Expand Up @@ -559,8 +559,29 @@ impl<'a> Runtime<'a> {

Ok(value)
}
DefaultFunction::EncodeUtf8 => todo!(),
DefaultFunction::DecodeUtf8 => todo!(),
DefaultFunction::EncodeUtf8 => {
let arg1 = self.args[0].unwrap_string()?;

let s_bytes = arg1.as_bytes();

let mut bytes = BumpVec::with_capacity_in(s_bytes.len(), arena);

bytes.extend_from_slice(s_bytes);

let value = Value::byte_string(arena, bytes);

Ok(value)
}
DefaultFunction::DecodeUtf8 => {
let arg1 = self.args[0].unwrap_byte_string()?;

let string = BumpString::from_utf8(arg1.clone())
.map_err(|e| MachineError::decode_utf8(e.utf8_error()))?;

let value = Value::string(arena, string);

Ok(value)
}
DefaultFunction::ChooseUnit => {
self.args[0].unwrap_unit()?;

Expand Down Expand Up @@ -638,7 +659,13 @@ impl<'a> Runtime<'a> {
Ok(value)
}
}
DefaultFunction::NullList => todo!(),
DefaultFunction::NullList => {
let (_, list) = self.args[0].unwrap_list()?;

let value = Value::bool(arena, list.is_empty());

Ok(value)
}
DefaultFunction::ChooseData => {
let con = self.args[0].unwrap_constant()?.unwrap_data()?;

Expand Down Expand Up @@ -791,7 +818,22 @@ impl<'a> Runtime<'a> {
Ok(value)
}
DefaultFunction::SerialiseData => todo!(),
DefaultFunction::MkPairData => todo!(),
DefaultFunction::MkPairData => {
let d1 = self.args[0].unwrap_constant()?.unwrap_data()?;
let d2 = self.args[1].unwrap_constant()?.unwrap_data()?;

let constant = Constant::proto_pair(
arena,
Type::data(arena),
Type::data(arena),
Constant::data(arena, d1),
Constant::data(arena, d2),
);

let value = Value::con(arena, constant);

Ok(value)
}
DefaultFunction::MkNilData => {
self.args[0].unwrap_unit()?;

Expand All @@ -802,7 +844,19 @@ impl<'a> Runtime<'a> {

Ok(value)
}
DefaultFunction::MkNilPairData => todo!(),
DefaultFunction::MkNilPairData => {
self.args[0].unwrap_unit()?;

let constant = Constant::proto_list(
arena,
Type::pair(arena, Type::data(arena), Type::data(arena)),
BumpVec::new_in(arena),
);

let value = Value::con(arena, constant);

Ok(value)
}
DefaultFunction::Bls12_381_G1_Add => todo!(),
DefaultFunction::Bls12_381_G1_Neg => todo!(),
DefaultFunction::Bls12_381_G1_ScalarMul => todo!(),
Expand Down
6 changes: 6 additions & 0 deletions crates/uplc/src/machine/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl<'a> Value<'a> {
Value::con(arena, con)
}

pub fn string(arena: &'a Bump, s: BumpString<'a>) -> &'a Value<'a> {
let con = arena.alloc(Constant::String(s));

Value::con(arena, con)
}

pub fn bool(arena: &'a Bump, b: bool) -> &'a Value<'a> {
let con = arena.alloc(Constant::Boolean(b));

Expand Down

0 comments on commit 72a91d4

Please sign in to comment.