Skip to content

Commit 52c177a

Browse files
committed
[Bug 14828] com.livecode.byte: Add "the byte with code _".
Add syntax for converting an integer to a single-byte `Data` value.
1 parent dd27e6a commit 52c177a

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

libscript/src/byte.mlc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public foreign handler MCByteExecDeleteFirstByteOf(inout Target as Data) as unde
5656

5757
public 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)
428430
end 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

432453
end module

libscript/src/module-byte.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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

tests/lcb/stdlib/byte.lcb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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

0 commit comments

Comments
 (0)