Skip to content

Commit

Permalink
use make_bytelike. minor refactor to be a classmethod
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Feb 19, 2025
1 parent cb2f184 commit 297a1b5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
8 changes: 1 addition & 7 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2296,15 +2296,9 @@ def build_IR(self, expr, args, kwargs, context):

schema = args_abi_t.selector_name().encode("utf-8")
schema_t = StringT(len(schema))
schema_buf = context.new_internal_variable(schema_t)
schema_buf = Expr._make_bytelike(context, StringT, schema)

ret = ["seq"]
ret.append(["mstore", schema_buf, len(schema)])

# TODO use Expr.make_bytelike, or better have a `bytestring` IRnode type
ret.append(
["mstore", add_ofst(schema_buf, 32), bytes_to_int(schema.ljust(32, b"\x00"))]
)

payload_buflen = args_abi_t.size_bound()
payload_t = BytesT(payload_buflen)
Expand Down
11 changes: 6 additions & 5 deletions vyper/codegen/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,22 @@ def parse_Hex(self):
# String literals
def parse_Str(self):
bytez = self.expr.value.encode("utf-8")
return self._make_bytelike(StringT, bytez)
return self._make_bytelike(self.context, StringT, bytez)

# Byte literals
def parse_Bytes(self):
return self._make_bytelike(BytesT, self.expr.value)
return self._make_bytelike(self.context, BytesT, self.expr.value)

def parse_HexBytes(self):
# HexBytes already has value as bytes
assert isinstance(self.expr.value, bytes)
return self._make_bytelike(BytesT, self.expr.value)
return self._make_bytelike(self.context, BytesT, self.expr.value)

def _make_bytelike(self, typeclass, bytez):
@classmethod
def _make_bytelike(cls, context, typeclass, bytez):
bytez_length = len(bytez)
btype = typeclass(bytez_length)
placeholder = self.context.new_internal_variable(btype)
placeholder = context.new_internal_variable(btype)
seq = []
seq.append(["mstore", placeholder, bytez_length])
for i in range(0, len(bytez), 32):
Expand Down

0 comments on commit 297a1b5

Please sign in to comment.