Skip to content

Commit

Permalink
simple c-style interface to facilitate integration with js when using…
Browse files Browse the repository at this point in the history
… emscripten

Task #153 - provide hooks for calling yacas from js when compiled with emscripten
  • Loading branch information
grzegorzmazur committed Jan 22, 2016
1 parent be74bdc commit 17ea425
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ endif ()


if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -std=c++11")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -std=c++11")
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4800")
add_definitions(-DYACAS_NO_CONSTEXPR -DYACAS_NO_ATOMIC_TYPES -DYACAS_UINT32_T_IN_GLOBAL_NAMESPACE)
endif ()

if (${CMAKE_SYSTEM_NAME} STREQUAL "Emscripten")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s DISABLE_EXCEPTION_CATCHING=0")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s DISABLE_EXCEPTION_CATCHING=0 -s ASSERTIONS=1")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --embed-file ${PROJECT_SOURCE_DIR}/scripts@/share/yacas/scripts")
endif ()

Expand Down Expand Up @@ -121,6 +121,8 @@ set (YACAS_WIN32_SOURCES src/win32commandline.cpp res/yacas.rc)
set (YACAS_UNIX_HEADERS include/yacas/unixcommandline.h)
set (YACAS_WIN32_HEADERS include/yacas/win32commandline.h)

set (YACAS_JS_INTERFACE_SOURCES src/js_interface.cpp)

if (UNIX)
set (YACAS_SOURCES ${YACAS_COMMON_SOURCES} ${YACAS_UNIX_SOURCES})
set (YACAS_HEADERS ${YACAS_UNIX_HEADERS})
Expand All @@ -129,6 +131,10 @@ else ()
set (YACAS_HEADERS ${YACAS_WIN32_HEADERS})
endif ()

if (${CMAKE_SYSTEM_NAME} STREQUAL "Emscripten")
set (YACAS_SOURCES ${YACAS_SOURCES} ${YACAS_JS_INTERFACE_SOURCES})
endif ()

set (YACAS_SCRIPTS
scripts/array.rep/code.ys
scripts/array.rep/code.ys.def
Expand Down
89 changes: 89 additions & 0 deletions src/js_interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* This file is part of yacas.
* Yacas is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesset General Public License as
* published by the Free Software Foundation, either version 2.1
* of the License, or (at your option) any later version.
*
* Yacas is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with yacas. If not, see <http://www.gnu.org/licenses/>.
*
*/

/*
* File: js_interface.cpp
* Author: mazur
*
* Created on January 19, 2016, 10:34 AM
*/

#include <emscripten.h>

#include <string>

#include "yacas/yacas.h"

namespace {
static CYacas* _yacas;
static std::string _yacas_result;
static std::string _yacas_side_effects;
static bool _yacas_is_error;
}

extern "C" bool EMSCRIPTEN_KEEPALIVE yacas_is_error()
{
return _yacas_is_error;
}

extern "C" char* EMSCRIPTEN_KEEPALIVE yacas_result()
{
std::size_t rn = _yacas_result.length();
char* r = new char[rn + 1];
for (std::size_t i = 0; i < rn; ++i)
r[i] = _yacas_result[i];

r[rn] = 0;

return r;
}

extern "C" char* EMSCRIPTEN_KEEPALIVE yacas_side_effects()
{
std::size_t rn = _yacas_side_effects.length();
char* r = new char[rn + 1];
for (std::size_t i = 0; i < rn; ++i)
r[i] = _yacas_side_effects[i];

r[rn] = 0;

return r;
}

extern "C" void EMSCRIPTEN_KEEPALIVE yacas_evaluate(const char* const p)
{
static std::stringstream os;
os.clear();
os.str("");

if (!_yacas) {
_yacas = new CYacas(os);
_yacas->Evaluate("DefaultDirectory(\"/share/yacas/scripts/\");");
_yacas->Evaluate("Load(\"yacasinit.ys\");");
}

_yacas->Evaluate(p);

_yacas_is_error = _yacas->IsError();

if (!_yacas_is_error)
_yacas_result = _yacas->Result();
else
_yacas_result = _yacas->Error();

_yacas_side_effects = os.str();
}

0 comments on commit 17ea425

Please sign in to comment.