Added default data locations to docs and other external tests.

This commit is contained in:
Chase McDermott
2018-07-14 16:42:43 -05:00
parent 31e56f9f99
commit 3267adcd14
32 changed files with 104 additions and 105 deletions
+4 -4
View File
@@ -193,9 +193,9 @@ Given the contract:
pragma solidity ^0.4.16;
contract Foo {
function bar(bytes3[2]) public pure {}
function bar(bytes3[2] memory) public pure {}
function baz(uint32 x, bool y) public pure returns (bool r) { r = x > 32 || y; }
function sam(bytes, bool, uint[]) public pure {}
function sam(bytes memory, bool, uint[] memory) public pure {}
}
@@ -490,8 +490,8 @@ As an example, the code
contract Test {
struct S { uint a; uint[] b; T[] c; }
struct T { uint x; uint y; }
function f(S s, T t, uint a) public { }
function g() public returns (S s, T t, uint a) {}
function f(S memory s, T memory t, uint a) public { }
function g() public returns (S memory s, T memory t, uint a) {}
}
would result in the JSON:
+4 -4
View File
@@ -54,7 +54,7 @@ idea is that assembly libraries will be used to enhance the language in such way
pragma solidity ^0.4.0;
library GetCode {
function at(address _addr) public view returns (bytes o_code) {
function at(address _addr) public view returns (bytes memory o_code) {
assembly {
// retrieve the size of the code, this needs assembly
let size := extcodesize(_addr)
@@ -83,7 +83,7 @@ you really know what you are doing.
library VectorSum {
// This function is less efficient because the optimizer currently fails to
// remove the bounds checks in array access.
function sumSolidity(uint[] _data) public view returns (uint o_sum) {
function sumSolidity(uint[] memory _data) public view returns (uint o_sum) {
for (uint i = 0; i < _data.length; ++i)
o_sum += _data[i];
}
@@ -91,7 +91,7 @@ you really know what you are doing.
// We know that we only access the array in bounds, so we can avoid the check.
// 0x20 needs to be added to an array because the first slot contains the
// array length.
function sumAsm(uint[] _data) public view returns (uint o_sum) {
function sumAsm(uint[] memory _data) public view returns (uint o_sum) {
for (uint i = 0; i < _data.length; ++i) {
assembly {
o_sum := add(o_sum, mload(add(add(_data, 0x20), mul(i, 0x20))))
@@ -100,7 +100,7 @@ you really know what you are doing.
}
// Same as above, but accomplish the entire code within inline assembly.
function sumPureAsm(uint[] _data) public view returns (uint o_sum) {
function sumPureAsm(uint[] memory _data) public view returns (uint o_sum) {
assembly {
// Load the length (first 32 bytes)
let len := mload(_data)
+3 -3
View File
@@ -1295,12 +1295,12 @@ custom types without the overhead of external function calls:
uint[] limbs;
}
function fromUint(uint x) internal pure returns (bigint r) {
function fromUint(uint x) internal pure returns (bigint memory r) {
r.limbs = new uint[](1);
r.limbs[0] = x;
}
function add(bigint _a, bigint _b) internal pure returns (bigint r) {
function add(bigint memory _a, bigint memory _b) internal pure returns (bigint memory r) {
r.limbs = new uint[](max(_a.limbs.length, _b.limbs.length));
uint carry = 0;
for (uint i = 0; i < r.limbs.length; ++i) {
@@ -1323,7 +1323,7 @@ custom types without the overhead of external function calls:
}
}
function limb(bigint _a, uint _limb) internal pure returns (uint) {
function limb(bigint memory _a, uint _limb) internal pure returns (uint) {
return _limb < _a.limbs.length ? _a.limbs[_limb] : 0;
}
+5 -6
View File
@@ -85,7 +85,7 @@ Example::
pragma solidity ^0.4.16;
contract C {
function f() public pure returns (uint8[5]) {
function f() public pure returns (uint8[5] memory) {
string[4] memory adaArr = ["This", "is", "an", "array"];
return ([1, 2, 3, 4, 5]);
}
@@ -445,7 +445,7 @@ independent copies will be created::
h(x);
}
function g(uint[20] y) internal pure {
function g(uint[20] memory y) internal pure {
y[2] = 3;
}
@@ -455,10 +455,9 @@ independent copies will be created::
}
The call to ``g(x)`` will not have an effect on ``x`` because it needs
to create an independent copy of the storage value in memory
(the default storage location is memory). On the other hand,
``h(x)`` successfully modifies ``x`` because only a reference
and not a copy is passed.
to create an independent copy of the storage value in memory.
On the other hand, ``h(x)`` successfully modifies ``x`` because only
a reference and not a copy is passed.
Sometimes, when I try to change the length of an array with ex: ``arrayname.length = 7;`` I get a compiler error ``Value must be an lvalue``. Why?
==================================================================================================================================================
+4 -4
View File
@@ -66,7 +66,7 @@ of votes.
Proposal[] public proposals;
/// Create a new ballot to choose one of `proposalNames`.
constructor(bytes32[] proposalNames) public {
constructor(bytes32[] memory proposalNames) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
@@ -452,9 +452,9 @@ high or low invalid bids.
/// correctly blinded invalid bids and for all bids except for
/// the totally highest.
function reveal(
uint[] _values,
bool[] _fake,
bytes32[] _secret
uint[] memory _values,
bool[] memory _fake,
bytes32[] memory _secret
)
public
onlyAfter(biddingEnd)
+10 -10
View File
@@ -471,11 +471,11 @@ Another example that uses external function types::
}
Request[] requests;
event NewRequest(uint);
function query(bytes data, function(bytes memory) external callback) public {
function query(bytes memory data, function(bytes memory) external callback) public {
requests.push(Request(data, callback));
emit NewRequest(requests.length - 1);
}
function reply(uint requestID, bytes response) public {
function reply(uint requestID, bytes memory response) public {
// Here goes the check that the reply comes from a trusted source
requests[requestID].callback(response);
}
@@ -486,7 +486,7 @@ Another example that uses external function types::
function buySomething() {
oracle.query("USD", this.oracleResponse);
}
function oracleResponse(bytes response) public {
function oracleResponse(bytes memory response) public {
require(
msg.sender == address(oracle),
"Only oracle can call this."
@@ -540,7 +540,7 @@ memory-stored reference type do not create a copy.
uint[] x; // the data location of x is storage
// the data location of memoryArray is memory
function f(uint[] memoryArray) public {
function f(uint[] memory memoryArray) public {
x = memoryArray; // works, copies the whole array to storage
uint[] storage y = x; // works, assigns a pointer, data location of y is storage
y[7]; // fine, returns the 8th element
@@ -557,7 +557,7 @@ memory-stored reference type do not create a copy.
}
function g(uint[] storage storageArray) internal {}
function h(uint[] memoryArray) public {}
function h(uint[] memory memoryArray) public {}
}
Summary
@@ -646,7 +646,7 @@ assigned to a variable right away.
function f() public pure {
g([uint(1), 2, 3]);
}
function g(uint[3] _data) public pure {
function g(uint[3] memory _data) public pure {
// ...
}
}
@@ -713,7 +713,7 @@ Members
bool[2][] m_pairsOfFlags;
// newPairs is stored in memory - the default for function arguments
function setAllFlagPairs(bool[2][] newPairs) public {
function setAllFlagPairs(bool[2][] memory newPairs) public {
// assignment to a storage array replaces the complete array
m_pairsOfFlags = newPairs;
}
@@ -739,7 +739,7 @@ Members
bytes m_byteData;
function byteArrays(bytes data) public {
function byteArrays(bytes memory data) public {
// byte arrays ("bytes") are different as they are stored without padding,
// but can be treated identical to "uint8[]"
m_byteData = data;
@@ -748,11 +748,11 @@ Members
delete m_byteData[2];
}
function addFlag(bool[2] flag) public returns (uint) {
function addFlag(bool[2] memory flag) public returns (uint) {
return m_pairsOfFlags.push(flag);
}
function createMemoryArray(uint size) public pure returns (bytes) {
function createMemoryArray(uint size) public pure returns (bytes memory) {
// Dynamic memory arrays are created using `new`:
uint[2][] memory arrayOfPairs = new uint[2][](size);
// Create a dynamic byte array: