Skip to content

Commit d9ae092

Browse files
authored
Merge pull request #269 from lucafabbian/develop
Making the library WebAssembly friendly
2 parents fbcb31a + ff1a440 commit d9ae092

13 files changed

Lines changed: 95 additions & 88 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*.la
1414
.dirstamp
1515

16+
# Wasm files
17+
*.wasm
18+
1619
# Executables
1720
hdt2rdf
1821
hdtInfo

libhdt/include/HDTSpecification.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class HDTSpecification
7272
*/
7373
const std::string& get(const std::string& key);
7474

75+
76+
/**
77+
* Get the value of a property, or "" if the key is not found.
78+
*/
79+
const std::string& getOrEmpty(const std::string& key);
80+
81+
7582
/** Set the value of a property */
7683
void set(const std::string& key, const std::string& value);
7784
};

libhdt/src/bitsequence/BitSequence375.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,25 @@ void BitSequence375::set(const size_t i, bool val) {
157157
array = &data[0];
158158
}
159159
if(val) {
160+
#ifdef __EMSCRIPTEN__
161+
/* Webassembly size_t differs from the one of
162+
other architectures.
163+
EDITED BY @lucafabbian, TODO: check */
164+
// fprintf(stderr, "[WARN] Unsafe cast called\n");
165+
bitset((uint64_t*) &array[0], i);
166+
#else
160167
bitset(&array[0], i);
168+
#endif
161169
} else {
170+
#ifdef __EMSCRIPTEN__
171+
/* Webassembly size_t differs from the one of
172+
other architectures.
173+
EDITED BY @lucafabbian, TODO: check */
174+
// fprintf(stderr, "[WARN] Unsafe cast called\n");
175+
bitclean((uint64_t*)&array[0], i);
176+
#else
162177
bitclean(&array[0], i);
178+
#endif
163179
}
164180

165181
numbits = i>=numbits ? i+1 : numbits;
@@ -173,7 +189,15 @@ void BitSequence375::append(bool bit) {
173189

174190
bool BitSequence375::access(const size_t i) const
175191
{
192+
#ifdef __EMSCRIPTEN__
193+
/* Webassembly size_t differs from the one of
194+
other architectures.
195+
EDITED BY @lucafabbian, TODO: check */
196+
//fprintf(stderr, "[WARN] Unsafe cast called\n");
197+
return bitget((uint64_t*)array, i);
198+
#else
176199
return bitget(array, i);
200+
#endif
177201
}
178202

179203
void BitSequence375::save(ostream & out) const

libhdt/src/dictionary/FourSectionDictionary.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ FourSectionDictionary::FourSectionDictionary(HDTSpecification & spec) : blocksiz
5656
objects = new csd::CSD_PFC();
5757
shared = new csd::CSD_PFC();
5858

59-
string blockSizeStr = "";
60-
try{
61-
blockSizeStr = spec.get("dict.block.size");
62-
}catch(exception& e){}
59+
string blockSizeStr = spec.getOrEmpty("dict.block.size");
6360

6461
if(!blockSizeStr.empty() && (blockSizeStr.find_first_not_of("0123456789") == string::npos))
6562
{

libhdt/src/dictionary/KyotoDictionary.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ KyotoDictionary::KyotoDictionary() {
5050
}
5151

5252
KyotoDictionary::KyotoDictionary(HDTSpecification &specification) : spec(specification) {
53-
string map = "";
54-
try{
55-
map = spec.get("dictionary.mapping");
56-
}catch(exception& e){}
53+
string map = spec.getOrEmpty("dictionary.mapping");
5754
if(map=="mapping1") {
5855
this->mapping = MAPPING1;
5956
} else {

libhdt/src/dictionary/LiteralDictionary.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ LiteralDictionary::LiteralDictionary(HDTSpecification & spec) : blocksize(8) {
5757
objectsLiterals = new csd::CSD_FMIndex();
5858
shared = new csd::CSD_PFC();
5959

60-
string blockSizeStr = "";
61-
try{
62-
blockSizeStr = spec.get("dict.block.size");
63-
}catch(exception& e){}
60+
string blockSizeStr = spec.getOrEmpty("dict.block.size");
61+
6462
if (blockSizeStr != "") {
6563
blocksize = atoi(blockSizeStr.c_str());
6664
}

libhdt/src/dictionary/PlainDictionary.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ PlainDictionary::PlainDictionary() {
5555
}
5656

5757
PlainDictionary::PlainDictionary(HDTSpecification &specification) : spec(specification) {
58-
string map ="";
59-
try{
60-
map = spec.get("dictionary.mapping");
61-
}catch(exception& e){}
58+
string map =spec.getOrEmpty("dictionary.mapping");
6259
if(map=="mapping1") {
6360
this->mapping = MAPPING1;
6461
} else {

libhdt/src/hdt/BasicHDT.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,8 @@ void BasicHDT::createComponents() {
9696

9797
// DICTIONARY
9898

99-
std::string dictType = "";
100-
try{
101-
dictType = spec.get("dictionary.type");
102-
}
103-
catch (std::exception& e){
104-
}
99+
std::string dictType = spec.getOrEmpty("dictionary.type");
100+
105101

106102
if(dictType==HDTVocabulary::DICTIONARY_TYPE_FOUR) {
107103
dictionary = new FourSectionDictionary(spec);
@@ -118,11 +114,8 @@ void BasicHDT::createComponents() {
118114
}
119115

120116
// TRIPLES
121-
std::string triplesType = "";
122-
try{
123-
triplesType = spec.get("triples.type");
124-
}catch (std::exception& e) {
125-
}
117+
std::string triplesType = spec.getOrEmpty("triples.type");
118+
126119
if(triplesType==HDTVocabulary::TRIPLES_TYPE_BITMAP) {
127120
triples = new BitmapTriples(spec);
128121
} else if(triplesType==HDTVocabulary::TRIPLES_TYPE_PLAIN) {
@@ -299,11 +292,8 @@ void BasicHDT::loadTriples(const char* fileName, const char* baseUri, RDFNotatio
299292
triplesList->stopProcessing(&iListener);
300293

301294
// SORT & Duplicates
302-
string ord = "";
303-
try{
304-
ord = spec.get("triplesOrder");
305-
}catch (std::exception& e){
306-
}
295+
string ord = spec.getOrEmpty("triplesOrder");
296+
307297
TripleComponentOrder order = parseOrder(
308298
ord.c_str());
309299
if (order == Unknown) {
@@ -581,12 +571,8 @@ void BasicHDT::loadTriplesFromHDTs(const char** fileNames, size_t numFiles, cons
581571
triplesList->stopProcessing(&iListener);
582572

583573
// SORT & Duplicates
584-
string ord = "";
585-
try{
586-
ord = spec.get("triplesOrder");
587-
}
588-
catch (std::exception& e){
589-
}
574+
string ord = spec.getOrEmpty("triplesOrder");
575+
590576
TripleComponentOrder order = parseOrder(ord.c_str());
591577
if (order == Unknown) {
592578
order = SPO;

libhdt/src/hdt/BasicModifiableHDT.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ BasicModifiableHDT::~BasicModifiableHDT() {
2727
}
2828

2929
void BasicModifiableHDT::createComponents() {
30-
try{
30+
#ifndef __EMSCRIPTEN__
31+
try {
3132
std::string dictType = spec.get("dictionary.type");
3233
std::string triplesType = spec.get("triples.type");
33-
}
34-
catch (std::exception& e)
35-
{
36-
}
34+
}catch (std::exception& e){ }
35+
36+
#endif
3737

3838
// FIXME: SELECT
3939
header = new PlainHeader();

libhdt/src/hdt/HDTSpecification.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ const std::string& HDTSpecification::get(const std::string& key) {
6363
return map.at(key);
6464
}
6565

66+
const std::string emptyString = "";
67+
68+
const std::string& HDTSpecification::getOrEmpty(const std::string& key) {
69+
70+
/* Webassembly does not play nice with C++
71+
72+
https://emscripten.org/docs/porting/exceptions.html
73+
https://stackoverflow.com/questions/69608789/c-exception-to-exception-less
74+
75+
*/
76+
#ifdef __EMSCRIPTEN__
77+
auto it = map.find(key);
78+
return it == map.end() ? emptyString : it->second;
79+
80+
#else
81+
try {
82+
return map.at(key);
83+
}catch (std::exception& e) {
84+
return emptyString;
85+
}
86+
#endif
87+
}
88+
6689
void HDTSpecification::set(const std::string& key, const std::string& value) {
6790
map[key] = value;
6891
}

0 commit comments

Comments
 (0)