From 7ea3d950d7001c12567c99aebdfc0fb73e46721e Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 27 Apr 2016 01:02:10 +0200 Subject: Allow calling internal functions of libraries. Internal functions of libraries can be called as if the library were a base contract of the calling contract. As the calling convention for internal functions is to not create a new call context, the code of these functions will be pulled into the context of the caller, duplicating their code. This might pull in code of further internal or even private functions. The use case for such functions is to allow libraries which can operate on memory types such that these types can also be modified in place. --- test/libsolidity/SolidityEndToEndTest.cpp | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index f9d2ab2b..6233e25f 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -6633,6 +6633,83 @@ BOOST_AUTO_TEST_CASE(delete_on_array_of_structs) BOOST_CHECK(callContractFunction("f()") == encodeArgs(true)); } +BOOST_AUTO_TEST_CASE(internal_library_function) +{ + // tests that internal library functions can be called from outside + // and retain the same memory context (i.e. are pulled into the caller's code) + char const* sourceCode = R"( + library L { + function f(uint[] _data) internal { + _data[3] = 2; + } + } + contract C { + function f() returns (uint) { + uint[] memory x = new uint[](7); + x[3] = 8; + L.f(x); + return x[3]; + } + } + )"; + // This has to work without linking, because everything will be inlined. + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(2))); +} + +BOOST_AUTO_TEST_CASE(internal_library_function_calling_private) +{ + // tests that internal library functions that are called from outside and that + // themselves call private functions are still able to (i.e. the private function + // also has to be pulled into the caller's code) + char const* sourceCode = R"( + library L { + function g(uint[] _data) private { + _data[3] = 2; + } + function f(uint[] _data) internal { + g(_data); + } + } + contract C { + function f() returns (uint) { + uint[] memory x = new uint[](7); + x[3] = 8; + L.f(x); + return x[3]; + } + } + )"; + // This has to work without linking, because everything will be inlined. + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(2))); +} + +BOOST_AUTO_TEST_CASE(internal_library_function_bound) +{ + char const* sourceCode = R"( + library L { + struct S { uint[] data; } + function f(S _s) internal { + _s.data[3] = 2; + } + } + contract C { + using L for L.S; + function f() returns (uint) { + L.S memory x; + x.data = new uint[](7); + x.data[3] = 8; + x.f(); + return x.data[3]; + } + } + )"; + // This has to work without linking, because everything will be inlined. + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(2))); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit