Skip to content

Commit f595148

Browse files
parseandmove exported method
1 parent 69b860d commit f595148

File tree

3 files changed

+123
-106
lines changed

3 files changed

+123
-106
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (WASM)
3434
COMPILE_FLAGS "-pthread"
3535
)
3636
set_target_properties(ChessEngine PROPERTIES
37-
LINK_FLAGS "-s WASM=1 -O3 --closure 1 -s MODULARIZE -s EXPORT_ES6=1 -s EXPORT_NAME=Engine -s ENVIRONMENT=web,worker -pthread -s PTHREAD_POOL_SIZE=8 -s INITIAL_MEMORY=600MB -s ALLOW_MEMORY_GROWTH -s EXPORTED_FUNCTIONS=['_main','_init','_move','_unmove','_listPieces','_getMoves','_getAttacks','_setFen','_runPerft','_eval','_getBestMove'] -s EXPORTED_RUNTIME_METHODS=['ccall','cwrap']"
37+
LINK_FLAGS "-s WASM=1 -O3 --closure 1 -s MODULARIZE -s EXPORT_ES6=1 -s EXPORT_NAME=Engine -s ENVIRONMENT=web,worker -pthread -s PTHREAD_POOL_SIZE=8 -s INITIAL_MEMORY=600MB -s ALLOW_MEMORY_GROWTH -s EXPORTED_FUNCTIONS=['_main','_init','_move','_parseandmove','_unmove','_listPieces','_getMoves','_getAttacks','_setFen','_runPerft','_eval','_getBestMove'] -s EXPORTED_RUNTIME_METHODS=['ccall','cwrap']"
3838
COMPILE_FLAGS "-pthread"
3939
)
4040
#-s ENVIRONMENT=web / -s ENVIRONMENT=node

main.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ char* move(const int start, const int end, int flag, int promotionType, int play
3737
return chararray;
3838
}
3939

40+
EMSCRIPTEN_KEEPALIVE
41+
char* parseandmove(const char* movestring){
42+
Move move = Interface::CLI::parseMove(movestring, board);
43+
board.makeMove(move);
44+
const std::string fen = board.fen();
45+
const int length = fen.length();
46+
char* chararray = new char[length + 1];
47+
strcpy(chararray, fen.c_str());
48+
return chararray;
49+
}
50+
4051
EMSCRIPTEN_KEEPALIVE
4152
char* unmove() {
4253
board.unMakeMove();

src/Logger.cpp

+111-105
Original file line numberDiff line numberDiff line change
@@ -3,157 +3,163 @@
33
#include <iostream>
44

55
#ifdef wasm
6+
67
#include "emscripten.h"
78
#include "emscripten/threading.h"
9+
810
#endif
911

1012
void Logger::start() {
11-
stop = false;
12-
empty = true;
13-
processingThread = std::thread(&Logger::threadFunc, this);
13+
stop = false;
14+
empty = true;
15+
processingThread = std::thread(&Logger::threadFunc, this);
1416
#ifdef logtofile
15-
logFile.open("log.txt");
17+
logFile.open("log.txt");
1618
#endif
1719
}
1820

1921
void Logger::end() {
20-
stop = true;
21-
processingThread.join();
22+
stop = true;
23+
processingThread.join();
2224
#ifdef logtofile
23-
logFile.close();
25+
logFile.close();
2426
#endif
2527
}
2628

27-
void Logger::log(const std::string&message) const {
28-
auto* newNode = new MessageNode();
29-
newNode->type = LOG;
30-
newNode->msg = message;
31-
tail->next = newNode;
32-
tail = newNode; {
33-
std::lock_guard lk(m);
34-
empty = false;
35-
}
36-
cv.notify_one();
29+
void Logger::log(const std::string &message) const {
30+
auto *newNode = new MessageNode();
31+
newNode->type = LOG;
32+
newNode->msg = message;
33+
tail->next = newNode;
34+
tail = newNode;
35+
{
36+
std::lock_guard lk(m);
37+
empty = false;
38+
}
39+
cv.notify_one();
3740
}
3841

39-
void Logger::logToFile(const std::string&message) const {
40-
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
42+
void Logger::logToFile(const std::string &message) const {
43+
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
4144
#ifdef logtofile
42-
auto* newNode = new MessageNode();
43-
newNode->type = TOFILE;
44-
newNode->msg = message;
45-
tail->next = newNode;
46-
tail = newNode; {
47-
std::lock_guard lk(m);
48-
empty = false;
49-
}
50-
cv.notify_one();
45+
auto* newNode = new MessageNode();
46+
newNode->type = TOFILE;
47+
newNode->msg = message;
48+
tail->next = newNode;
49+
tail = newNode; {
50+
std::lock_guard lk(m);
51+
empty = false;
52+
}
53+
cv.notify_one();
5154
#endif
5255
}
5356

54-
void Logger::sendInt(const std::string&name, const int value) const {
55-
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
57+
void Logger::sendInt(const std::string &name, const int value) const {
58+
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
5659
#ifdef wasm
57-
auto* newNode = new IntNode();
58-
newNode->type = INT;
59-
newNode->name = name;
60-
newNode->value = value;
61-
tail->next = newNode;
62-
tail = newNode;
63-
{
64-
std::lock_guard lk(m);
65-
empty = false;
66-
}
67-
cv.notify_one();
60+
auto *newNode = new IntNode();
61+
newNode->type = INT;
62+
newNode->name = name;
63+
newNode->value = value;
64+
tail->next = newNode;
65+
tail = newNode;
66+
{
67+
std::lock_guard lk(m);
68+
empty = false;
69+
}
70+
cv.notify_one();
6871
#endif
6972
}
7073

71-
void Logger::sendString(const std::string&name, const std::string&value) const {
72-
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
74+
void Logger::sendString(const std::string &name, const std::string &value) const {
75+
// Since the function doesn't do anything with the flag disabled the compiler should technically remove it and all the calls to it
7376
#ifdef wasm
74-
auto* newNode = new StringNode();
75-
newNode->type = STRING;
76-
newNode->name = name;
77-
newNode->value = value;
78-
tail->next = newNode;
79-
tail = newNode;
80-
{
81-
std::lock_guard lk(m);
82-
empty = false;
83-
}
84-
cv.notify_one();
77+
auto *newNode = new StringNode();
78+
newNode->type = STRING;
79+
newNode->name = name;
80+
newNode->value = value;
81+
tail->next = newNode;
82+
tail = newNode;
83+
{
84+
std::lock_guard lk(m);
85+
empty = false;
86+
}
87+
cv.notify_one();
8588
#endif
8689
}
8790

8891

8992
Logger::Logger() {
90-
this->head = new MessageNode();
91-
this->tail = this->head;
92-
stop = false;
93-
empty = true;
93+
this->head = new MessageNode();
94+
this->tail = this->head;
95+
stop = false;
96+
empty = true;
9497
}
9598

9699
Logger::~Logger() {
97-
delete this->head;
100+
delete this->head;
98101
}
99102

100103
void Logger::threadFunc() {
101-
while (!stop) {
102-
// Keep processing the buffer, wait a bit when it's empty
103-
if (!processNode()) {
104-
std::unique_lock lk(m);
105-
empty = true;
106-
cv.wait(lk, [this] { return !empty || stop; });
107-
lk.unlock();
108-
}
109-
}
110-
// Empty the buffer
111-
while (processNode()) {
112-
}
104+
while (!stop) {
105+
// Keep processing the buffer, wait a bit when it's empty
106+
if (!processNode()) {
107+
std::unique_lock lk(m);
108+
empty = true;
109+
cv.wait(lk, [this] { return !empty || stop; });
110+
lk.unlock();
111+
}
112+
}
113+
// Empty the buffer
114+
while (processNode()) {
115+
}
113116
}
114117

115118
bool Logger::processNode() {
116-
if (head->next != nullptr) {
117-
Node* newHead = head->next;
119+
if (head->next != nullptr) {
120+
Node *newHead = head->next;
118121

119122
#ifndef wasm
120-
if (newHead->type == LOG) {
121-
std::cout << reinterpret_cast<MessageNode *>(newHead)->msg;
122-
}
123+
if (newHead->type == LOG) {
124+
std::cout << reinterpret_cast<MessageNode *>(newHead)->msg;
125+
}
123126
#ifdef logtofile
124-
else if (newHead->type == TOFILE) {
125-
logFile << reinterpret_cast<MessageNode *>(newHead)->msg;
126-
}
127+
else if (newHead->type == TOFILE) {
128+
logFile << reinterpret_cast<MessageNode *>(newHead)->msg;
129+
}
127130
#endif
128131
#endif
129132

130133
#ifdef wasm
131-
switch (newHead->type) {
132-
case LOG: {
133-
MAIN_THREAD_EM_ASM(
134-
postMessage({data: [UTF8ToString($0)], name: "log"})
135-
, reinterpret_cast<MessageNode *>(newHead)->msg.c_str());
136-
break;
137-
}
138-
case STRING: {
139-
MAIN_THREAD_EM_ASM(
140-
postMessage({data: [UTF8ToString($1)], name: UTF8ToString($0)})
141-
, reinterpret_cast<StringNode *>(newHead)->name.c_str(), reinterpret_cast<StringNode *>(newHead)->value.c_str());
142-
break;
143-
}
144-
case INT: {
145-
MAIN_THREAD_EM_ASM(
146-
postMessage({data: [$1], name: UTF8ToString($0)})
147-
, reinterpret_cast<IntNode *>(newHead)->name.c_str(), reinterpret_cast<IntNode *>(newHead)->value);
148-
break;
149-
}
150-
default: break;
151-
}
134+
switch (newHead->type) {
135+
case LOG: {
136+
MAIN_THREAD_EM_ASM(
137+
postMessage({data:[UTF8ToString($0)], name: "log"}),
138+
reinterpret_cast<MessageNode *>(newHead)->msg.c_str());
139+
break;
140+
}
141+
case STRING: {
142+
MAIN_THREAD_EM_ASM(
143+
postMessage({data:[UTF8ToString($1)], name: UTF8ToString($0)}),
144+
reinterpret_cast<StringNode *>(newHead)->name.c_str(),
145+
reinterpret_cast<StringNode *>(newHead)->value.c_str());
146+
break;
147+
}
148+
case INT: {
149+
MAIN_THREAD_EM_ASM(
150+
postMessage({data:[$1], name: UTF8ToString($0)}),
151+
reinterpret_cast<IntNode *>(newHead)->name.c_str(),
152+
reinterpret_cast<IntNode *>(newHead)->value);
153+
break;
154+
}
155+
default:
156+
break;
157+
}
152158
#endif
153159

154-
delete head;
155-
head = newHead;
156-
return true;
157-
}
158-
return false;
160+
delete head;
161+
head = newHead;
162+
return true;
163+
}
164+
return false;
159165
}

0 commit comments

Comments
 (0)