Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berry class int64 made immutable #20727

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
### Breaking Changed

### Changed
- Berry class `int64` made immutable

### Fixed

Expand Down
108 changes: 94 additions & 14 deletions lib/libesp32/berry_int64/src/be_int64_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,23 @@ int64_t* int64_fromstring(bvm *vm, const char* s) {
}
BE_FUNC_CTYPE_DECLARE(int64_fromstring, "int64", "@s")

// is the int64 within int32 range?
bbool int64_isint(int64_t *i64) {
return (*i64 >= INT32_MIN && *i64 <= INT32_MAX);
}
BE_FUNC_CTYPE_DECLARE(int64_isint, "b", ".")

int32_t int64_toint(int64_t *i64) {
return (int32_t) *i64;
}
BE_FUNC_CTYPE_DECLARE(int64_toint, "i", ".")

void int64_set(int64_t *i64, int32_t high, int32_t low) {
*i64 = ((int64_t)high << 32) | ((int64_t)low & 0xFFFFFFFF);
}
BE_FUNC_CTYPE_DECLARE(int64_set, "", ".ii")

int64_t* int64_fromu32(bvm *vm, uint32_t low) {
int64_t* int64_fromu32(bvm *vm, uint32_t low, uint32_t high) {
int64_t* r64 = (int64_t*)be_malloc(vm, sizeof(int64_t));
*r64 = low;
*r64 = low | (((int64_t)high) << 32);
return r64;
}
BE_FUNC_CTYPE_DECLARE(int64_fromu32, "int64", "@i")
BE_FUNC_CTYPE_DECLARE(int64_fromu32, "int64", "@i[i]")

int64_t* int64_add(bvm *vm, int64_t *i64, int64_t *j64) {
int64_t* r64 = (int64_t*)be_malloc(vm, sizeof(int64_t));
Expand All @@ -87,6 +88,14 @@ int64_t* int64_add(bvm *vm, int64_t *i64, int64_t *j64) {
}
BE_FUNC_CTYPE_DECLARE(int64_add, "int64", "@(int64)(int64)")

int64_t* int64_add32(bvm *vm, int64_t *i64, int32_t j32) {
int64_t* r64 = (int64_t*)be_malloc(vm, sizeof(int64_t));
// it's possible that arg j64 is nullptr, since class type does allow NULLPTR to come through.
*r64 = *i64 + j32;
return r64;
}
BE_FUNC_CTYPE_DECLARE(int64_add32, "int64", "@(int64)i")

int64_t* int64_sub(bvm *vm, int64_t *i64, int64_t *j64) {
int64_t* r64 = (int64_t*)be_malloc(vm, sizeof(int64_t));
// it's possible that arg j64 is nullptr, since class type does allow NULLPTR to come through.
Expand Down Expand Up @@ -179,22 +188,90 @@ bbool int64_lte(int64_t *i64, int64_t *j64) {
}
BE_FUNC_CTYPE_DECLARE(int64_lte, "b", ".(int64)")

bbool int64_tobool(int64_t *i64) {
return *i64 != 0;
}
BE_FUNC_CTYPE_DECLARE(int64_tobool, "b", ".")

void* int64_tobytes(int64_t *i64, size_t *len) {
if (len) { *len = sizeof(int64_t); }
return i64;
}
BE_FUNC_CTYPE_DECLARE(int64_tobytes, "&", ".")

void int64_frombytes(int64_t *i64, uint8_t* ptr, size_t len, int32_t idx) {
int64_t* int64_frombytes(bvm *vm, uint8_t* ptr, size_t len, int32_t idx) {
int64_t* r64 = (int64_t*)be_malloc(vm, sizeof(int64_t));
if (idx < 0) { idx = len + idx; } // support negative index, counting from the end
if (idx < 0) { idx = 0; } // sanity check
if (idx > len) { idx = len; }
uint32_t usable_len = len - idx;
if (usable_len > sizeof(int64_t)) { usable_len = sizeof(int64_t); }
*i64 = 0; // start with 0
memmove(i64, ptr + idx, usable_len);
*r64 = 0; // start with 0
memmove(r64, ptr + idx, usable_len);
return r64;
}
BE_FUNC_CTYPE_DECLARE(int64_frombytes, "", ".(bytes)~[i]")
BE_FUNC_CTYPE_DECLARE(int64_frombytes, "int64", "@(bytes)~[i]")

/*

def toint64(i)
if (type(i) == 'int') return int64.fromu32(i) end
if (type(i) == 'instance') && isinstance(i, int64) return i end
return nil
end

*/

/********************************************************************
** Solidified function: toint64
********************************************************************/
be_local_closure(toint64, /* name */
be_nested_proto(
4, /* nstack */
1, /* argc */
0, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 4]) { /* constants */
/* K0 */ be_nested_str(int),
/* K1 */ be_nested_str(int64),
/* K2 */ be_nested_str(fromu32),
/* K3 */ be_nested_str(instance),
}),
&be_const_str_to64,
&be_const_str_solidified,
( &(const binstruction[23]) { /* code */
0x60040004, // 0000 GETGBL R1 G4
0x5C080000, // 0001 MOVE R2 R0
0x7C040200, // 0002 CALL R1 1
0x1C040300, // 0003 EQ R1 R1 K0
0x78060004, // 0004 JMPF R1 #000A
0xB8060200, // 0005 GETNGBL R1 K1
0x8C040302, // 0006 GETMET R1 R1 K2
0x5C0C0000, // 0007 MOVE R3 R0
0x7C040400, // 0008 CALL R1 2
0x80040200, // 0009 RET 1 R1
0x60040004, // 000A GETGBL R1 G4
0x5C080000, // 000B MOVE R2 R0
0x7C040200, // 000C CALL R1 1
0x1C040303, // 000D EQ R1 R1 K3
0x78060005, // 000E JMPF R1 #0015
0x6004000F, // 000F GETGBL R1 G15
0x5C080000, // 0010 MOVE R2 R0
0xB80E0200, // 0011 GETNGBL R3 K1
0x7C040400, // 0012 CALL R1 2
0x78060000, // 0013 JMPF R1 #0015
0x80040000, // 0014 RET 1 R0
0x4C040000, // 0015 LDNIL R1
0x80040200, // 0016 RET 1 R1
})
)
);
/*******************************************************************/


#include "be_fixed_be_class_int64.h"

Expand All @@ -203,13 +280,16 @@ class be_class_int64 (scope: global, name: int64) {
_p, var
init, ctype_func(int64_init)
deinit, ctype_func(int64_deinit)
set, ctype_func(int64_set)
fromu32, static_ctype_func(int64_fromu32)
toint64, static_closure(toint64_closure)

tostring, ctype_func(int64_tostring)
fromstring, static_ctype_func(int64_fromstring)
isint, ctype_func(int64_isint)
toint, ctype_func(int64_toint)
tobool, ctype_func(int64_tobool)

add, ctype_func(int64_add32)
+, ctype_func(int64_add)
-, ctype_func(int64_sub)
*, ctype_func(int64_mul)
Expand All @@ -224,6 +304,6 @@ class be_class_int64 (scope: global, name: int64) {
<=, ctype_func(int64_lte)

tobytes, ctype_func(int64_tobytes)
frombytes, ctype_func(int64_frombytes)
frombytes, static_ctype_func(int64_frombytes)
}
@const_object_info_end */
43 changes: 18 additions & 25 deletions lib/libesp32/berry_int64/tests/int64.be
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@ assert(int(int64(-5)) == -5)
assert(str(int64(-5)) == "-5")

# testing large numbers
a = int64()
a.set(0x7FFFFFFF,0xFFFFFFFF) # max positive number
assert(str(a) == "9223372036854775807")

a.set(0x80000000,0x00000000)
assert(str(a) == "-9223372036854775808")

a.set(10,10)
assert(str(a) == "42949672970")
assert(str(int64.fromu32(0xFFFFFFFF, 0x7FFFFFFF)) == "9223372036854775807") # max positive number
assert(str(int64.fromu32(0x00000000, 0x80000000)) == "-9223372036854775808")
assert(str(int64.fromu32(10,10)) == "42949672970")

# addition
assert(str(int64(10) + int64(20)) == "30")
Expand Down Expand Up @@ -97,19 +91,18 @@ assert(a.tobytes() == bytes("0000000000000080"))
assert(int64(-1).tobytes() == bytes("FFFFFFFFFFFFFFFF"))

# frombytes
a = int64()
a.frombytes(bytes("0A00000000000000"), 0)
assert(a.tobytes() == bytes("0A00000000000000"))
a.frombytes(bytes("0A00000000000000")) # with implicit index 0
assert(a.tobytes() == bytes("0A00000000000000"))
a.frombytes(bytes("0A00000000000000"), 1) # index 1 and incomplete (7 bytes)
assert(a.tobytes() == bytes("0000000000000000"))

a.frombytes(bytes("00FFFFFFFFFFFFFFFF"), 1) # index 1 and incomplete (7 bytes)
assert(a.tobytes() == bytes("FFFFFFFFFFFFFFFF"))
a.frombytes(bytes("00FFFFFFFFFFFFFFFF"), -2) # from end
assert(a.tobytes() == bytes("FFFF000000000000"))
a.frombytes(bytes("")) # empty
assert(a.tobytes() == bytes("0000000000000000"))
a.frombytes(bytes(""),4) # empty with wrong index
assert(a.tobytes() == bytes("0000000000000000"))
assert(int64.frombytes(bytes("0A00000000000000"), 0) == bytes("0A00000000000000")) # with implicit index 0
assert(int64.frombytes(bytes("0A00000000000000")) == bytes("0A00000000000000"))
assert(int64.frombytes(bytes("0A00000000000000"), 1) == bytes("0000000000000000")) # index 1 and incomplete (7 bytes)

assert(int64.frombytes(bytes("00FFFFFFFFFFFFFFFF"), 1) == bytes("FFFFFFFFFFFFFFFF")) # index 1 and incomplete (7 bytes)
assert(int64.frombytes(bytes("00FFFFFFFFFFFFFFFF"), -2) == bytes("FFFF000000000000")) # from end
assert(int64.frombytes(bytes("")) == bytes("0000000000000000")) # empty
assert(int64.frombytes(bytes(""),4) == bytes("0000000000000000")) # empty with wrong index

# fromu32
assert(int64.fromu32(0).tobytes() == bytes("0000000000000000"))
assert(int64.fromu32(0xFFFFFFFF).tobytes() == bytes("FFFFFFFF00000000"))
assert(int64.fromu32(0xFFFFFFFF, 1).tobytes() == bytes("FFFFFFFF01000000"))
assert(int64.fromu32(-1, 1).tobytes() == bytes("FFFFFFFF01000000"))
assert(int64.fromu32(-1, -1).tobytes() == bytes("FFFFFFFFFFFFFFFF"))
8 changes: 2 additions & 6 deletions lib/libesp32/berry_matter/src/embedded/Matter_Fabric.be
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,10 @@ class Matter_Fabric : Matter_Expirable
def get_fabric_index() return self.fabric_index end

def get_fabric_id_as_int64()
var i64 = int64()
i64.frombytes(self.fabric_id)
return i64
return int64.frombytes(self.fabric_id)
end
def get_device_id_as_int64()
var i64 = int64()
i64.frombytes(self.device_id)
return i64
return int64.frombytes(self.device_id)
end

def get_admin_vendor_name()
Expand Down
3 changes: 1 addition & 2 deletions lib/libesp32/berry_matter/src/embedded/Matter_TLV.be
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ class Matter_TLV
var item_len = TLV._len[item_type]

if item_len == 8 # i64 / u64 / double
self.val = int64()
self.val.frombytes(b, idx)
self.val = int64.frombytes(b, idx)
idx += 8
elif item_type == TLV.BFALSE || item_type == TLV.BTRUE # bool
self.val = (item_type == TLV.BTRUE)
Expand Down
26 changes: 12 additions & 14 deletions lib/libesp32/berry_matter/src/solidify/solidified_Matter_Fabric.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern const bclass be_class_Matter_Fabric;
********************************************************************/
be_local_closure(Matter_Fabric_get_device_id_as_int64, /* name */
be_nested_proto(
5, /* nstack */
4, /* nstack */
1, /* argc */
2, /* varg */
0, /* has upvals */
Expand All @@ -26,13 +26,12 @@ be_local_closure(Matter_Fabric_get_device_id_as_int64, /* name */
}),
be_str_weak(get_device_id_as_int64),
&be_const_str_solidified,
( &(const binstruction[ 6]) { /* code */
( &(const binstruction[ 5]) { /* code */
0xB8060000, // 0000 GETNGBL R1 K0
0x7C040000, // 0001 CALL R1 0
0x8C080301, // 0002 GETMET R2 R1 K1
0x88100102, // 0003 GETMBR R4 R0 K2
0x7C080400, // 0004 CALL R2 2
0x80040200, // 0005 RET 1 R1
0x8C040301, // 0001 GETMET R1 R1 K1
0x880C0102, // 0002 GETMBR R3 R0 K2
0x7C040400, // 0003 CALL R1 2
0x80040200, // 0004 RET 1 R1
})
)
);
Expand Down Expand Up @@ -1065,7 +1064,7 @@ be_local_closure(Matter_Fabric_get_ipk_group_key, /* name */
********************************************************************/
be_local_closure(Matter_Fabric_get_fabric_id_as_int64, /* name */
be_nested_proto(
5, /* nstack */
4, /* nstack */
1, /* argc */
2, /* varg */
0, /* has upvals */
Expand All @@ -1080,13 +1079,12 @@ be_local_closure(Matter_Fabric_get_fabric_id_as_int64, /* name */
}),
be_str_weak(get_fabric_id_as_int64),
&be_const_str_solidified,
( &(const binstruction[ 6]) { /* code */
( &(const binstruction[ 5]) { /* code */
0xB8060000, // 0000 GETNGBL R1 K0
0x7C040000, // 0001 CALL R1 0
0x8C080301, // 0002 GETMET R2 R1 K1
0x88100102, // 0003 GETMBR R4 R0 K2
0x7C080400, // 0004 CALL R2 2
0x80040200, // 0005 RET 1 R1
0x8C040301, // 0001 GETMET R1 R1 K1
0x880C0102, // 0002 GETMBR R3 R0 K2
0x7C040400, // 0003 CALL R1 2
0x80040200, // 0004 RET 1 R1
})
)
);
Expand Down
Loading