File tree Expand file tree Collapse file tree 3 files changed +79
-1
lines changed
Expand file tree Collapse file tree 3 files changed +79
-1
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,8 @@ public foreign handler MCByteExecDeleteFirstByteOf(inout Target as Data) as unde
5656
5757public foreign handler MCDataExecRandomBytes(in Count as LCUIndex, out Value as Data) as undefined binds to "<builtin>"
5858
59+ public foreign handler MCByteEvalByteWithCode(in Value as LCUInt, out Result as Data) as undefined binds to "<builtin>"
60+
5961--
6062
6163/*
@@ -427,6 +429,25 @@ begin
427429 MCDataExecRandomBytes(Count, output)
428430end syntax
429431
430- --
432+ ----------------------------------------------------------------
433+ -- Conversion between bytes and numbers
434+ ----------------------------------------------------------------
435+
436+ /*
437+ Summary: Create a 1-byte data value from a number
438+
439+ Value: The value for the new data value
440+
441+ Description:
442+ Returns a byte of binary data, created using the given value. The
443+ <Value> must be between 0 and 255 (inclusive).
444+
445+ Tags: Binary
446+ */
447+ syntax ByteWithCode is prefix operator with precedence 2
448+ "the" "byte" "with" "code" <Value: Expression>
449+ begin
450+ MCByteEvalByteWithCode(Value, output)
451+ end syntax
431452
432453end module
Original file line number Diff line number Diff line change @@ -217,6 +217,25 @@ MCDataExecRandomBytes (uindex_t p_count, MCDataRef & r_data)
217217 /* UNCHECKED */ MCSRandomData (p_count, r_data);
218218}
219219
220+ // //////////////////////////////////////////////////////////////////////////////
221+
222+ extern " C" MC_DLLEXPORT void
223+ MCByteEvalByteWithCode (uinteger_t p_value,
224+ MCDataRef & r_data)
225+ {
226+ if (p_value > BYTE_MAX)
227+ {
228+ MCErrorCreateAndThrow (kMCGenericErrorTypeInfo ,
229+ " reason" , MCSTR (" overflow in byte operation" ),
230+ nil);
231+ return ;
232+ }
233+
234+ const byte_t t_byte = p_value;
235+
236+ MCDataCreateWithBytes (&t_byte, 1 , r_data);
237+ }
238+
220239// //////////////////////////////////////////////////////////////////////////////////////////////////
221240
222241#ifdef _TEST
Original file line number Diff line number Diff line change 1+ /*
2+ Copyright (C) 2015 Runtime Revolution Ltd.
3+
4+ This file is part of LiveCode.
5+
6+ LiveCode is free software; you can redistribute it and/or modify it under
7+ the terms of the GNU General Public License v3 as published by the Free
8+ Software Foundation.
9+
10+ LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
11+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+ for more details.
14+
15+ You should have received a copy of the GNU General Public License
16+ along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
17+
18+ module com.livecode.foreign.tests
19+
20+ use com.livecode.__INTERNAL._testlib
21+
22+ handler TestByteWithCode_Negative()
23+ return the byte with code -1
24+ end handler
25+ handler TestByteWithCode_Overflow()
26+ return the byte with code 256
27+ end handler
28+ public handler TestByteWithCode()
29+ variable t
30+ put the byte with code 0 into t
31+
32+ test "code->byte" when the number of bytes in t is 1
33+
34+ MCUnitTestHandlerThrows(TestByteWithCode_Negative, "code->byte (negative)")
35+ MCUnitTestHandlerThrows(TestByteWithCode_Overflow, "code->byte (overflow)")
36+ end handler
37+
38+ end module
You can’t perform that action at this time.
0 commit comments