Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 110795a

Browse files
committed
[[feature-random]] MLC: Add "random bytes" expression.
Add a new expression for random data generation, e.g. put 436 random bytes into tData
1 parent 6df6994 commit 110795a

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

libscript/src/byte.mlc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public foreign handler MCByteExecDeleteByteRangeOf(in Start as index, in Finish
5252
public foreign handler MCByteExecDeleteLastByteOf(inout Target as data) as undefined binds to "<builtin>"
5353
public foreign handler MCByteExecDeleteFirstByteOf(inout Target as data) as undefined binds to "<builtin>"
5454

55+
public foreign handler MCDataExecRandomBytes(in Count as uindex, out Value as data) as undefined binds to "<builtin>"
56+
5557
--
5658

5759
/*
@@ -406,4 +408,23 @@ end syntax
406408

407409
--
408410

411+
/*
412+
Summary: Generate random data
413+
Count: Expression evaluating to the number of bytes to generate
414+
415+
Description:
416+
Generates the requested number of bytes of cryptographic-quality
417+
random data.
418+
419+
Tags: Binary, Random
420+
421+
*/
422+
syntax RandomBytes is postfix operator with precedence 5
423+
<Count : Expression> "random" "bytes"
424+
begin
425+
MCDataExecRandomBytes(Count, output)
426+
end syntax
427+
428+
--
429+
409430
end module

libscript/src/module-byte.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ extern "C" MC_DLLEXPORT bool MCByteRepeatForEachByte(void*& x_iterator, MCDataRe
204204
return true;
205205
}
206206

207+
////////////////////////////////////////////////////////////////////////////////
208+
209+
extern "C" MC_DLLEXPORT void
210+
MCDataExecRandomBytes (uindex_t p_count, MCDataRef & r_data)
211+
{
212+
/* UNCHECKED */ MCDataCreateRandom (p_count, r_data);
213+
}
214+
207215
////////////////////////////////////////////////////////////////////////////////////////////////////
208216

209217
#ifdef _TEST

0 commit comments

Comments
 (0)