Skip to content

Commit 6730398

Browse files
committed
Fix js interface because of c++ updated.
1 parent 93c80e9 commit 6730398

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

examples/main.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ const axiom3 = new rule_t(
3131

3232
const premise = new rule_t("(! (! X))");
3333
const target = new rule_t("X");
34-
const target_hash = target.data_string();
34+
const target_hash = target.key();
3535

3636
function main() {
3737
let rules = {};
3838
let facts = {};
3939

4040
let cycle = -1;
41-
rules[mp.data_string()] = [mp, cycle];
42-
facts[axiom1.data_string()] = [axiom1, cycle];
43-
facts[axiom2.data_string()] = [axiom2, cycle];
44-
facts[axiom3.data_string()] = [axiom3, cycle];
45-
facts[premise.data_string()] = [premise, cycle];
41+
rules[mp.key()] = [mp, cycle];
42+
facts[axiom1.key()] = [axiom1, cycle];
43+
facts[axiom2.key()] = [axiom2, cycle];
44+
facts[axiom3.key()] = [axiom3, cycle];
45+
facts[premise.key()] = [premise, cycle];
4646

4747
while (true) {
4848
let temp_rules = {};
@@ -59,7 +59,7 @@ function main() {
5959
if (candidate === null) {
6060
continue;
6161
}
62-
const candidate_hash = candidate.data_string();
62+
const candidate_hash = candidate.key();
6363
if (candidate.length() != 0) {
6464
// rule
6565
if (candidate_hash in rules || candidate_hash in temp_rules) {
@@ -84,11 +84,11 @@ function main() {
8484
cycle++;
8585
for (let r_hash in temp_rules) {
8686
const rule = temp_rules[r_hash];
87-
rules[rule.data_string()] = [rule, cycle];
87+
rules[rule.key()] = [rule, cycle];
8888
}
8989
for (let f_hash in temp_facts) {
9090
const fact = temp_facts[f_hash];
91-
facts[fact.data_string()] = [fact, cycle];
91+
facts[fact.key()] = [fact, cycle];
9292
}
9393
}
9494
}

jsds/ds.cc

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#include <ds/ds.hh>
22
#include <emscripten/bind.h>
3+
#include <vector>
34

45
namespace em = emscripten;
56

7+
// 由于embind的限制,这里无法使用string_view。
8+
// 为了保持一致性,一律使用复制。
69
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> {
811
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]] {
1114
operator delete(result);
1215
return std::unique_ptr<T>(nullptr);
1316
}
@@ -18,7 +21,7 @@ template<typename T>
1821
auto to_string(T* value, int buffer_size) -> std::string {
1922
auto result = reinterpret_cast<char*>(operator new(buffer_size));
2023
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]] {
2225
operator delete(result);
2326
return std::string();
2427
}
@@ -29,15 +32,18 @@ auto to_string(T* value, int buffer_size) -> std::string {
2932
}
3033

3134
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());
3538
return dst;
3639
}
3740

3841
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;
4147
}
4248

4349
template<typename T>
@@ -57,23 +63,23 @@ auto common_declaration(em::class_<T>& t) {
5763
t.class_function("from_string", from_string<T>);
5864
t.class_function("to_string", to_string<T>, em::allow_raw_pointers());
5965
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());
6167
t.function("clone", clone<T>, em::allow_raw_pointers());
6268
t.function("data_size", data_size<T>, em::allow_raw_pointers());
6369
}
6470

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> {
6672
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]] {
6874
operator delete(result);
6975
return std::unique_ptr<ds::term_t>(nullptr);
7076
}
7177
return std::unique_ptr<ds::term_t>(result);
7278
}
7379

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> {
7581
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]] {
7783
operator delete(result);
7884
return std::unique_ptr<ds::rule_t>(nullptr);
7985
}
@@ -82,14 +88,16 @@ auto rule_ground(ds::rule_t* rule, ds::rule_t* dictionary, int length) -> std::u
8288

8389
auto rule_match(ds::rule_t* rule_1, ds::rule_t* rule_2, int length) -> std::unique_ptr<ds::rule_t> {
8490
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]] {
8692
operator delete(result);
8793
return std::unique_ptr<ds::rule_t>(nullptr);
8894
}
8995
return std::unique_ptr<ds::rule_t>(result);
9096
}
9197

9298
EMSCRIPTEN_BINDINGS(ds) {
99+
em::register_vector<std::uint8_t>("Buffer");
100+
93101
auto string_t = em::class_<ds::string_t>("String");
94102
auto item_t = em::class_<ds::item_t>("Item");
95103
auto variable_t = em::class_<ds::variable_t>("Variable");

jsds/jsds.mjs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class _common_t {
2626
} else if (value instanceof this.type) {
2727
this.value = value;
2828
this.capacity = size;
29-
} else if (value instanceof Uint8Array) {
29+
} else if (value instanceof ds.Buffer) {
3030
this.value = this.type.from_binary(value);
3131
this.capacity = this.size();
3232
if (size != 0) {
@@ -55,18 +55,17 @@ class _common_t {
5555
return this.type.to_binary(this.value);
5656
}
5757

58-
data_string() {
59-
const decoder = new TextDecoder("utf-8");
60-
return decoder.decode(this.data());
61-
}
62-
6358
size() {
6459
return this.value.data_size();
6560
}
6661

6762
copy() {
6863
return new this.constructor(this.value.clone(), this.size());
6964
}
65+
66+
key() {
67+
return this.toString();
68+
}
7069
}
7170

7271
export class string_t extends _common_t {
@@ -127,9 +126,9 @@ export class term_t extends _common_t {
127126
}
128127
}
129128

130-
ground(other) {
129+
ground(other, scope = null) {
131130
const capacity = buffer_size();
132-
const term = this.type.ground(this.value, other.value, capacity);
131+
const term = this.type.ground(this.value, other.value, scope, capacity);
133132
if (term == null) {
134133
return null;
135134
}
@@ -154,9 +153,9 @@ export class rule_t extends _common_t {
154153
return new term_t(this.value.conclusion());
155154
}
156155

157-
ground(other) {
156+
ground(other, scope = null) {
158157
const capacity = buffer_size();
159-
const rule = this.type.ground(this.value, other.value, capacity);
158+
const rule = this.type.ground(this.value, other.value, scope, capacity);
160159
if (rule == null) {
161160
return null;
162161
}

0 commit comments

Comments
 (0)