Skip to content

Commit 62c00de

Browse files
committed
feat: add mp support forshare
1 parent 9a1271b commit 62c00de

File tree

14 files changed

+100
-40
lines changed

14 files changed

+100
-40
lines changed

scafi3-integration/src/test/scala/it/unibo/scafi/integration/PlatformTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ object PlatformTest:
104104

105105
class SubstitutionBuilder:
106106
private var _substitutions = Set.empty[Substitution]
107-
private[PlatformTest] def add(key: Pattern, value: String): Unit = _substitutions += ((key, value))
107+
private[PlatformTest] def add(key: Pattern, value: String): Unit = _substitutions += (key, value)
108108
def substitutions: Set[Substitution] = _substitutions.view.toSet
109109

110110
extension (pattern: Pattern)

scafi3-mp-api/js/src/main/scala/it/unibo/scafi/libraries/FullLibrary.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import scala.scalajs.js.annotation.JSExportTopLevel
55

66
import it.unibo.scafi.language.AggregateFoundation
77
import it.unibo.scafi.language.common.syntax.BranchingSyntax
8+
import it.unibo.scafi.language.fc.syntax.FieldCalculusSyntax
89
import it.unibo.scafi.language.xc.FieldBasedSharedData
910
import it.unibo.scafi.language.xc.syntax.ExchangeSyntax
1011
import it.unibo.scafi.message.JSBinaryCodable.jsBinaryCodable
@@ -18,7 +19,7 @@ import it.unibo.scafi.types.{ EqWrapper, JSTypes }
1819
@SuppressWarnings(Array("scalafix:DisableSyntax.asInstanceOf"))
1920
@JSExportTopLevel("FullLibrary")
2021
class FullLibrary(using
21-
lang: AggregateFoundation & ExchangeSyntax & BranchingSyntax & FieldBasedSharedData,
22+
lang: AggregateFoundation & ExchangeSyntax & BranchingSyntax & FieldBasedSharedData & FieldCalculusSyntax,
2223
) extends FullPortableLibrary
2324
with JSFieldBasedSharedData
2425
with JSTypes:

scafi3-mp-api/native/src/main/resources/include/internals.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
#include <stdlib.h>
55
#include <stdbool.h>
6+
#include "message.h"
67

78
typedef struct MapImpl* Map;
89

910
Map map_empty();
10-
void map_put(Map map, const void* key, const void* value);
11-
void* map_get(const Map map, const void* key);
11+
void* map_put(Map map, const Eq* key, const void* value);
12+
void* map_get(const Map map, const Eq* key);
1213
size_t map_size(const Map map);
1314
void map_free(Map map);
1415

scafi3-mp-api/native/src/main/resources/include/libraries.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ static inline ReturnSending* return_sending(const Field* r, const Field* s) {
2626
typedef struct AggregateLibrary {
2727
FieldBasedSharedData Field;
2828
const BinaryCodable* (*local_id)(void);
29-
const Field* (*device)(void);
3029
const void* (*branch)(bool condition, const void* (*true_branch)(void), const void* (*false_branch)(void));
3130
const Field* (*exchange)(const Field* initial, ReturnSending* (*f)(const Field* in));
31+
const BinaryCodable* (*share)(const BinaryCodable* initial, const BinaryCodable* (*f)(const Field* nvalues));
3232
} AggregateLibrary;
3333

3434
#endif // LIBRARIES_H

scafi3-mp-api/native/src/main/resources/include/message.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
#include <stdbool.h>
66
#include <stddef.h>
77

8+
typedef struct Eq {
9+
bool (*cmp)(const void* a, const void* b);
10+
uint32_t (*hash)(const void* data);
11+
} Eq;
12+
813
typedef struct BinaryCodable {
14+
Eq eq;
915
char* type_name;
1016
const uint8_t* (*encode)(const void *data, size_t *encoded_size);
1117
const void* (*decode)(const uint8_t *buffer, size_t size);
12-
bool (*cmp)(const void* a, const void* b);
13-
uint32_t (*hash)(const void* data);
1418
char* (*to_str)(const void* data);
1519
} BinaryCodable;
1620

scafi3-mp-api/native/src/main/resources/include/utils.h

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,28 @@
1414

1515
#define fn lambda
1616

17-
#define MAP_OF(Name, KeysTypePtr, ValuesTypePtr) \
18-
typedef Map Name; \
19-
static inline Name Name##_empty() { \
20-
return map_empty(); \
21-
} \
22-
static inline void Name##_put(Name map, const KeysTypePtr key, const ValuesTypePtr value) { \
23-
map_put(map, key, value); \
24-
} \
25-
static inline ValuesTypePtr Name##_get(const Name map, const KeysTypePtr key) { \
26-
return (ValuesTypePtr)map_get(map, key); \
27-
} \
28-
static inline size_t Name##_size(const Name map) { \
29-
return map_size(map); \
30-
} \
31-
static inline void Name##_free(Name map) { \
32-
map_free(map); \
17+
#define EQ_CAST(key) _Generic((key), \
18+
const Eq*: (const Eq*)(key), \
19+
Eq*: (const Eq*)(key), \
20+
const BinaryCodable*: (const Eq*)(key), \
21+
BinaryCodable*: (const Eq*)(key))
22+
23+
#define MAP_OF(Name, KeysTypePtr, ValuesTypePtr) \
24+
typedef Map Name; \
25+
static inline Name Name##_empty() { \
26+
return map_empty(); \
27+
} \
28+
static inline ValuesTypePtr Name##_put(Name map, const KeysTypePtr key, const ValuesTypePtr value) { \
29+
return (ValuesTypePtr)map_put(map, EQ_CAST(key), value); \
30+
} \
31+
static inline ValuesTypePtr Name##_get(const Name map, const KeysTypePtr key) { \
32+
return (ValuesTypePtr)map_get(map, EQ_CAST(key)); \
33+
} \
34+
static inline size_t Name##_size(const Name map) { \
35+
return map_size(map); \
36+
} \
37+
static inline void Name##_free(Name map) { \
38+
map_free(map); \
3339
}
3440

3541
#endif // UTILS_H

scafi3-mp-api/native/src/main/resources/utils/int_codable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ static void init_int_value(Int* iv, int value) {
5959
iv->base.type_name = "number";
6060
iv->base.encode = int_value_encode;
6161
iv->base.decode = int_value_decode;
62-
iv->base.cmp = int_value_cmp;
63-
iv->base.hash = int_value_hash;
62+
iv->base.eq.cmp = int_value_cmp;
63+
iv->base.eq.hash = int_value_hash;
6464
iv->base.to_str = int_value_to_str;
6565
iv->value = value;
6666
}

scafi3-mp-api/native/src/main/resources/utils/protobuf_codable.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ ProtobufValue* protobuf_value_create(
6565
pv->base.type_name = (char*)message->descriptor->name;
6666
pv->base.encode = protobuf_encode;
6767
pv->base.decode = decode_func;
68-
pv->base.cmp = protobuf_cmp;
69-
pv->base.hash = protobuf_hash;
68+
pv->base.eq.cmp = protobuf_cmp;
69+
pv->base.eq.hash = protobuf_hash;
7070
pv->base.to_str = to_str_func ? to_str_func : protobuf_default_to_str;
7171
pv->message = message;
7272
pv->descriptor = message->descriptor;
@@ -80,4 +80,4 @@ void protobuf_value_free(ProtobufValue* pv) {
8080
}
8181
free(pv);
8282
}
83-
}
83+
}

scafi3-mp-api/native/src/main/resources/utils/protobuf_codable.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@ typedef struct ProtobufValue {
2020
#define DEFINE_PROTOBUF_MESSAGE(type_name, to_str_func) \
2121
static char* (*type_name##_to_str_ptr)(const void*) = to_str_func; \
2222
static const void* decode_##type_name(const uint8_t* buffer, size_t size) { \
23-
if (!buffer || size == 0) return NULL; \
24-
void* msg = type_name##__unpack(NULL, size, buffer); \
23+
if (!buffer) return NULL; \
24+
void* msg = NULL; \
25+
if (size == 0) { \
26+
/* Empty buffer means all fields have default values */ \
27+
msg = calloc(1, type_name##__descriptor.sizeof_message); \
28+
if (msg) { \
29+
((ProtobufCMessage*)msg)->descriptor = &type_name##__descriptor; \
30+
} \
31+
} else { \
32+
msg = type_name##__unpack(NULL, size, buffer); \
33+
} \
2534
return msg ? protobuf_value_create((ProtobufCMessage*)msg, decode_##type_name, type_name##_to_str_ptr) : NULL; \
2635
}
2736

scafi3-mp-api/native/src/main/scala/it/unibo/scafi/libraries/FullLibrary.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import scala.scalanative.unsafe.{ Ptr, Zone }
66

77
import it.unibo.scafi.language.AggregateFoundation
88
import it.unibo.scafi.language.common.syntax.BranchingSyntax
9+
import it.unibo.scafi.language.fc.syntax.FieldCalculusSyntax
910
import it.unibo.scafi.language.xc.FieldBasedSharedData
1011
import it.unibo.scafi.language.xc.syntax.ExchangeSyntax
1112
import it.unibo.scafi.libraries.FullLibrary.libraryRef
@@ -21,7 +22,7 @@ import it.unibo.scafi.types.{ CMap, EqWrapper, NativeTypes }
2122

2223
@SuppressWarnings(Array("scalafix:DisableSyntax.asInstanceOf"))
2324
class FullLibrary(using
24-
lang: AggregateFoundation & ExchangeSyntax & BranchingSyntax & FieldBasedSharedData,
25+
lang: AggregateFoundation & ExchangeSyntax & BranchingSyntax & FieldBasedSharedData & FieldCalculusSyntax,
2526
) extends FullPortableLibrary
2627
with NativeFieldBasedSharedData
2728
with NativeTypes:
@@ -46,6 +47,8 @@ class FullLibrary(using
4647
libraryRef.get().branch_(condition)(trueBranch)(falseBranch)
4748
(!lib).exchange = (initial: Ptr[CField], f: Function1[Ptr[CField], ReturnSending]) =>
4849
libraryRef.get().exchange_(initial)(f)
50+
(!lib).share = (initial: Ptr[CBinaryCodable], f: Function1[Ptr[CField], Ptr[CBinaryCodable]]) =>
51+
libraryRef.get().share_(initial)(f)
4952
lib
5053
end FullLibrary
5154

0 commit comments

Comments
 (0)