github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/stringlib/lua/packing.quotas.lua (about) 1 -- string.pack 2 do 3 local s = "a" 4 local s1000 = ("a"):rep(1000) 5 6 -- string.pack uses memory to store its output 7 local ctx = runtime.callcontext({kill={memory=4000}}, string.pack, "ssss", s1000, s, s, s) 8 print(ctx) 9 --> =done 10 11 print(runtime.callcontext({kill={memory=4000}}, string.pack, "ssss", s1000, s1000, s1000, s1000)) 12 --> =killed 13 14 -- string.pack uses cpu to produce its output 15 ctx = runtime.callcontext({kill={cpu=400}}, string.pack, "ssss", s1000, s, s, s) 16 print(ctx) 17 --> =done 18 19 print(runtime.callcontext({kill={cpu=400}}, string.pack, "ssss", s1000, s1000, s1000, s1000)) 20 --> =killed 21 end 22 23 -- string.unpack 24 do 25 local fmt = "i" 26 local packed = string.pack(fmt, 100) 27 28 print(string.unpack(fmt:rep(5), packed:rep(5))) 29 --> ~100 100 100 100 100 .* 30 31 -- string.unpack uses memory to produce its output 32 local ctx = runtime.callcontext({kill={memory=1000}}, string.unpack, fmt:rep(20), packed:rep(20)) 33 print(ctx) 34 --> =done 35 36 print(runtime.callcontext({kill={memory=1000}}, string.unpack, fmt:rep(100), packed:rep(100))) 37 --> =killed 38 39 -- string.unpack uses cpu to produce its output 40 local ctx = runtime.callcontext({kill={cpu=100}}, string.unpack, fmt:rep(50), packed:rep(50)) 41 print(ctx) 42 --> =done 43 44 print(runtime.callcontext({kill={cpu=100}}, string.unpack, fmt:rep(500), packed:rep(500))) 45 --> =killed 46 47 local fmt = "s" 48 local packed10 = string.pack(fmt, ("a"):rep(10)) 49 local packed1000 = string.pack(fmt, ("a"):rep(1000)) 50 51 -- big strings need lots of memory 52 local ctx = runtime.callcontext({kill={memory=2000}}, string.unpack, fmt:rep(20), packed10:rep(20)) 53 print(ctx) 54 --> =done 55 56 print(runtime.callcontext({kill={memory=2000}}, string.unpack, fmt:rep(20), packed1000:rep(20))) 57 --> =killed 58 end