1
1
#include < ds/ds.hh>
2
2
#include < emscripten/bind.h>
3
+ #include < vector>
3
4
4
5
namespace em = emscripten;
5
6
7
+ // 由于embind的限制,这里无法使用string_view。
8
+ // 为了保持一致性,一律使用复制。
6
9
template <typename T>
7
- auto from_string (const std::string& text , int buffer_size) -> std::unique_ptr<T> {
10
+ auto from_string (const std::string& string , int buffer_size) -> std::unique_ptr<T> {
8
11
auto result = reinterpret_cast <T*>(operator new (buffer_size));
9
- auto scan_result = result->scan (text. c_str (), reinterpret_cast <std::byte*>(result) + buffer_size);
10
- if (scan_result == nullptr ) {
12
+ auto scan_result = result->scan (string. data (), reinterpret_cast <std::byte*>(result) + buffer_size);
13
+ if (scan_result == nullptr ) [[unlikely]] {
11
14
operator delete (result);
12
15
return std::unique_ptr<T>(nullptr );
13
16
}
@@ -18,7 +21,7 @@ template<typename T>
18
21
auto to_string (T* value, int buffer_size) -> std::string {
19
22
auto result = reinterpret_cast <char *>(operator new (buffer_size));
20
23
auto print_result = value->print (result, reinterpret_cast <char *>(result) + buffer_size);
21
- if (print_result == nullptr || print_result - result == buffer_size) {
24
+ if (print_result == nullptr || print_result - result == buffer_size) [[unlikely]] {
22
25
operator delete (result);
23
26
return std::string ();
24
27
}
@@ -29,15 +32,18 @@ auto to_string(T* value, int buffer_size) -> std::string {
29
32
}
30
33
31
34
template <typename T>
32
- auto from_binary (const std::string& text ) -> std::unique_ptr<T> {
33
- auto dst = std::unique_ptr<T>(reinterpret_cast <T*>(operator new (text .size ())));
34
- memcpy (dst.get (), text .data (), text .size ());
35
+ auto from_binary (const std::vector<std:: uint8_t >& binary ) -> std::unique_ptr<T> {
36
+ auto dst = std::unique_ptr<T>(reinterpret_cast <T*>(operator new (binary .size ())));
37
+ memcpy (dst.get (), binary .data (), binary .size ());
35
38
return dst;
36
39
}
37
40
38
41
template <typename T>
39
- auto to_binary (T* value) -> em::val {
40
- return em::val (em::typed_memory_view (value->data_size (), reinterpret_cast <unsigned char *>(value)));
42
+ auto to_binary (T* value) -> std::vector<std::uint8_t> {
43
+ std::vector<std::uint8_t > result;
44
+ result.resize (value->data_size ());
45
+ memcpy (result.data (), value, value->data_size ());
46
+ return result;
41
47
}
42
48
43
49
template <typename T>
@@ -57,23 +63,23 @@ auto common_declaration(em::class_<T>& t) {
57
63
t.class_function (" from_string" , from_string<T>);
58
64
t.class_function (" to_string" , to_string<T>, em::allow_raw_pointers ());
59
65
t.class_function (" from_binary" , from_binary<T>);
60
- t.class_function (" to_binary" , to_binary<T>, em::return_value_policy::reference ());
66
+ t.class_function (" to_binary" , to_binary<T>, em::allow_raw_pointers ());
61
67
t.function (" clone" , clone<T>, em::allow_raw_pointers ());
62
68
t.function (" data_size" , data_size<T>, em::allow_raw_pointers ());
63
69
}
64
70
65
- auto term_ground (ds::term_t * term, ds::term_t * dictionary, int length) -> std::unique_ptr<ds::term_t> {
71
+ auto term_ground (ds::term_t * term, ds::term_t * dictionary, const char * scope, int length) -> std::unique_ptr<ds::term_t> {
66
72
auto result = reinterpret_cast <ds::term_t *>(operator new (length));
67
- if (result->ground (term, dictionary, reinterpret_cast <std::byte*>(result) + length) == nullptr ) {
73
+ if (result->ground (term, dictionary, scope, reinterpret_cast <std::byte*>(result) + length) == nullptr ) [[unlikely]] {
68
74
operator delete (result);
69
75
return std::unique_ptr<ds::term_t >(nullptr );
70
76
}
71
77
return std::unique_ptr<ds::term_t >(result);
72
78
}
73
79
74
- auto rule_ground (ds::rule_t * rule, ds::rule_t * dictionary, int length) -> std::unique_ptr<ds::rule_t> {
80
+ auto rule_ground (ds::rule_t * rule, ds::rule_t * dictionary, const char * scope, int length) -> std::unique_ptr<ds::rule_t> {
75
81
ds::rule_t * result = reinterpret_cast <ds::rule_t *>(operator new (length));
76
- if (result->ground (rule, dictionary, reinterpret_cast <std::byte*>(result) + length) == nullptr ) {
82
+ if (result->ground (rule, dictionary, scope, reinterpret_cast <std::byte*>(result) + length) == nullptr ) [[unlikely]] {
77
83
operator delete (result);
78
84
return std::unique_ptr<ds::rule_t >(nullptr );
79
85
}
@@ -82,14 +88,16 @@ auto rule_ground(ds::rule_t* rule, ds::rule_t* dictionary, int length) -> std::u
82
88
83
89
auto rule_match (ds::rule_t * rule_1, ds::rule_t * rule_2, int length) -> std::unique_ptr<ds::rule_t> {
84
90
ds::rule_t * result = reinterpret_cast <ds::rule_t *>(operator new (length));
85
- if (result->match (rule_1, rule_2, reinterpret_cast <std::byte*>(result) + length) == nullptr ) {
91
+ if (result->match (rule_1, rule_2, reinterpret_cast <std::byte*>(result) + length) == nullptr ) [[unlikely]] {
86
92
operator delete (result);
87
93
return std::unique_ptr<ds::rule_t >(nullptr );
88
94
}
89
95
return std::unique_ptr<ds::rule_t >(result);
90
96
}
91
97
92
98
EMSCRIPTEN_BINDINGS (ds) {
99
+ em::register_vector<std::uint8_t >(" Buffer" );
100
+
93
101
auto string_t = em::class_<ds::string_t >(" String" );
94
102
auto item_t = em::class_<ds::item_t >(" Item" );
95
103
auto variable_t = em::class_<ds::variable_t >(" Variable" );
0 commit comments