11#include < emscripten/bind.h>
2+ #include < emscripten/val.h>
23
3- #include " RtBot.pb.h"
44#include " rtbot/Message.h"
5+ #include " rtbot/OperatorJson.h"
56#include " rtbot/bindings.h"
67
78using namespace emscripten ;
9+ using json = nlohmann::json;
10+ using timestamp_t = rtbot::timestamp_t ;
811
912namespace emscripten {
1013namespace internal {
1114
15+ // Vector binding type for converting between C++ vectors and JavaScript arrays
1216template <typename T, typename Allocator>
1317struct BindingType <std::vector<T, Allocator>> {
1418 using ValBinding = BindingType<val>;
@@ -21,6 +25,7 @@ struct BindingType<std::vector<T, Allocator>> {
2125 }
2226};
2327
28+ // TypeID specialization for vectors to ensure proper type handling
2429template <typename T>
2530struct TypeID <
2631 T, typename std::enable_if_t <std::is_same<typename Canonicalized<T>::type,
@@ -32,41 +37,77 @@ struct TypeID<
3237} // namespace internal
3338} // namespace emscripten
3439
35- void test (rtbot::api::proto::Input const & input) { std::cout << " Input: " << input.DebugString () << std::endl; }
36-
37- string processBatch32 (string const & programId, vector<uint32_t > times32, vector<double > values,
38- vector<string> const & ports) {
39- // translate passed 32 bit timestamp into 64 bit internal type
40- vector<uint64_t > times (times32.begin (), times32.end ());
41- return processBatch (programId, times, values, ports);
40+ // Helper function to process batches with 32-bit timestamps
41+ std::string processBatch32 (const std::string& programId, const std::vector<uint32_t >& times32,
42+ const std::vector<double >& values, const std::vector<std::string>& ports) {
43+ // Convert 32-bit timestamps to 64-bit
44+ std::vector<uint64_t > times (times32.begin (), times32.end ());
45+ return rtbot::process_batch (programId, times, values, ports);
4246}
4347
44- string processBatch32Debug (string const & programId, vector< uint32_t > times32, vector< double > values,
45- vector<string> const & ports) {
46- // translate passed 32 bit timestamp into 64 bit internal type
47- vector<uint64_t > times (times32.begin (), times32.end ());
48- return processBatchDebug (programId, times, values, ports);
48+ // Debug version of batch processing
49+ std::string processBatch32Debug ( const std::string& programId, const std:: vector<uint32_t >& times32,
50+ const std::vector< double >& values, const std::vector<std::string>& ports) {
51+ std:: vector<uint64_t > times (times32.begin (), times32.end ());
52+ return rtbot::process_batch_debug (programId, times, values, ports);
4953}
5054
51- EMSCRIPTEN_BINDINGS (RtBot) {
52- value_object<rtbot::Message< std::uint64_t , double >>( " Message " )
53- . field ( " time " , & rtbot::Message<std:: uint64_t , double >:: time)
54- . field ( " value " , &rtbot::Message<std:: uint64_t , double >::value);
55+ // Helper to add a single message to the buffer
56+ std::string addMessage ( const std::string& programId, const std::string& portId, uint32_t time, double value) {
57+ return rtbot::add_to_message_buffer (programId, portId, static_cast < uint64_t >( time), value);
58+ }
5559
56- emscripten::function (" validate" , &validate);
57- emscripten::function (" validateOperator" , &validateOperator);
60+ namespace {
61+ // Helper functions for message creation and access
62+ timestamp_t getMessage_getTime (const rtbot::Message<rtbot::NumberData>& msg) { return msg.time ; }
5863
59- emscripten::function (" createProgram" , &createProgram);
60- emscripten::function (" deleteProgram" , &deleteProgram);
64+ void getMessage_setTime (rtbot::Message<rtbot::NumberData>& msg, timestamp_t t) { msg.time = t; }
6165
62- emscripten::function (" addToMessageBuffer" , &addToMessageBuffer);
63- emscripten::function (" processMessageBuffer" , &processMessageBuffer);
64- emscripten::function (" processMessageBufferDebug" , &processMessageBufferDebug);
66+ const rtbot::NumberData& getMessage_getData (const rtbot::Message<rtbot::NumberData>& msg) { return msg.data ; }
6567
66- emscripten::function (" getProgramEntryOperatorId" , &getProgramEntryOperatorId);
67- emscripten::function (" getProgramEntryPorts" , &getProgramEntryPorts);
68- emscripten::function (" getProgramOutputFilter" , &getProgramOutputFilter);
68+ void getMessage_setData (rtbot::Message<rtbot::NumberData>& msg, const rtbot::NumberData& data) { msg.data = data; }
69+ } // namespace
6970
70- emscripten::function (" processBatch" , &processBatch32);
71- emscripten::function (" processBatchDebug" , &processBatch32Debug);
72- }
71+ EMSCRIPTEN_BINDINGS (RtBot) {
72+ // Register NumberData type first
73+ value_object<rtbot::NumberData>(" NumberData" ).field (" value" , &rtbot::NumberData::value);
74+
75+ // Register Message type with manual accessors to avoid base class issues
76+ class_<rtbot::Message<rtbot::NumberData>>(" Message" )
77+ .constructor <timestamp_t , const rtbot::NumberData&>()
78+ .property (" time" , &getMessage_getTime, &getMessage_setTime)
79+ .property (" data" , &getMessage_getData, &getMessage_setData);
80+
81+ // Core program management functions
82+ function (" createProgram" , &rtbot::create_program);
83+ function (" deleteProgram" , &rtbot::delete_program);
84+ function (" validateProgram" , &rtbot::validate_program);
85+ function (" validateOperator" , &rtbot::validate_operator);
86+
87+ // Message handling
88+ function (" addToMessageBuffer" , &addMessage);
89+ function (" processMessageBuffer" , &rtbot::process_message_buffer);
90+ function (" processMessageBufferDebug" , &rtbot::process_message_buffer_debug);
91+
92+ // Program information
93+ function (" getProgramEntryOperatorId" , &rtbot::get_program_entry_operator_id);
94+ function (" getProgramEntryPorts" , optional_override ([](const std::string& programId) -> std::string {
95+ try {
96+ auto entry_id = rtbot::get_program_entry_operator_id (programId);
97+ if (entry_id.empty ()) return " []" ;
98+
99+ // By default, return ["i1"] as the entry port
100+ json ports = {" i1" };
101+ return ports.dump ();
102+ } catch (const std::exception& e) {
103+ return " []" ;
104+ }
105+ }));
106+
107+ // Batch processing
108+ function (" processBatch" , &processBatch32);
109+ function (" processBatchDebug" , &processBatch32Debug);
110+
111+ // State management
112+ // TODO: Serialize and deserialize program state
113+ }
0 commit comments