Experimental Stable Memory Module Example

The library of Motoko lastest version add a new module : Experimental Stable Memory.
There is a example
The code :

import D "mo:base/Debug";
import N32 "mo:base/Nat32";
import S "mo:base/ExperimentalStableMemory";
import T "mo:base/Text";


actor{

    let metadata = T.encodeUtf8("metadata");
    let size = N32.fromNat(metadata.size());
    public func grow() : async Nat32{ S.grow(1:Nat32) };
    public func store() : async (){
        S.storeBlob(1:Nat32, metadata);
    };
    public func load() : async (){
        D.print(switch(T.decodeUtf8(S.loadBlob(1, N32.toNat(size)))){
            case null { "load error" };
            case (?t){ t };
        })
    };
    public func offset_store() : async (){
        S.storeBlob((1+size):Nat32, metadata)
    };
    public func offset_load() : async (){
        D.print(switch(T.decodeUtf8(S.loadBlob(1+size, N32.toNat(size)))){
            case null { "load error" };
            case (?t){ t };
        })
    };
    public func multi_load() : async (){
        D.print(switch(T.decodeUtf8(S.loadBlob(1, N32.toNat(size+size)))){
            case null { "load error" };
            case (?t){ t };
        })
    };
}
4 Likes

What failure are you seeing?

sorry my fault I use debug…

The number of wasm page should be 65536. But I can not allocate 65536 pages in grow function, what’s wrong with it?