Skip to content
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

add compile-time evaluation of slice() #3667

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
71 changes: 71 additions & 0 deletions tests/functional/builtins/codegen/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,74 @@ def test_slice_bytes32_calldata_extended(get_contract, code, result):
c.bar(3, "0x0001020304050607080910111213141516171819202122232425262728293031", 5).hex()
== result
)


code_compruntime = [
(
"bytes32",
"""
@external
@view
def baz() -> Bytes[16]:
return slice(0x1234567891234567891234567891234567891234567891234567891234567891, 0, 16)
""",
"""
@external
@view
def baz(val: bytes32) -> Bytes[16]:
return slice(val, 0, 16)
""",
"0x1234567891234567891234567891234567891234567891234567891234567891",
"12345678912345678912345678912345",
),
(
"string",
"""
@external
@view
def baz() -> String[5]:
return slice("why hello! how are you?", 4, 5)
""",
"""
@external
@view
def baz(val: String[100]) -> String[5]:
return slice(val, 4, 5)
""",
"why hello! how are you?",
"hello",
),
(
"bytes",
"""
@external
@view
def baz() -> Bytes[6]:
return slice(b'gm sir, how are you ?', 0, 6)
""",
"""
@external
@view
def baz(val: Bytes[100]) -> Bytes[6]:
return slice(val, 0, 6)
""",
b"gm sir, how are you ?",
"gm sir".encode("utf-8").hex(),
),
]


@pytest.mark.parametrize(
"name,compcode,runcode,arg,result", code_compruntime, ids=[el[0] for el in code_compruntime]
)
def test_comptime_runtime(get_contract, name, compcode, runcode, arg, result):
c1 = get_contract(compcode)
c2 = get_contract(runcode)
ret1 = c1.baz()
ret2 = c2.baz(arg)
if hasattr(ret1, "hex"):
assert ret1.hex() == result
assert ret2.hex() == result
else:
assert ret1 == result
assert ret2 == result
24 changes: 24 additions & 0 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,30 @@ class Slice(BuiltinFunction):
]
_return_type = None

def evaluate(self, node):
(lit, st, le) = node.args[:3]
if (
isinstance(lit, (vy_ast.Bytes, vy_ast.Str, vy_ast.Hex))
and isinstance(st, vy_ast.Int)
and isinstance(le, vy_ast.Int)
):
(st_val, le_val) = (st.value, le.value)
if isinstance(lit, vy_ast.Bytes):
sublit = lit.value[st_val : (st_val + le_val)]
return vy_ast.Bytes.from_node(node, value=sublit)
elif isinstance(lit, vy_ast.Str):
sublit = lit.value[st_val : (st_val + le_val)]
return vy_ast.Str.from_node(node, value=sublit)
else:
length = len(lit.value) // 2 - 1
if length != 32:
# TODO unreachable?
raise UnfoldableNode
sublit = lit.value[st_val : (2 + st_val + (le_val * 2))]
return vy_ast.Bytes.from_node(node, value=sublit)
else:
raise UnfoldableNode

def fetch_call_return(self, node):
arg_type, _, _ = self.infer_arg_types(node)

Expand Down