Attempting to convert R functions written in C to JavaScript using Emscripten is proving to be a challenging task. The primary objective at this stage is to migrate a function called pf
.
The source code for the function can be accessed here. As I navigate to the source directory and execute the command:
(trunk)⚡ % emcc -s EXPORTED_FUNCTIONS="['_Rf_pf']" nmath/pf.c
warning: unresolved symbol: Rf_pbeta
warning: unresolved symbol: Rf_pchisq
warning: unresolved symbol: R_NaN
warning: unresolved symbol: R_NegInf
warning: unresolved symbol: R_PosInf
The resulting JavaScript output contains the function _Rf_pf
, which can be called successfully, albeit with limitations. The lack of resolution for symbols such as R_PosInf
causes an issue where ML_POSINF
evaluates to 0, impacting the algorithm's functionality.
Is there a solution available to resolve these symbols and facilitate a seamless porting of the function?
Further attempts to compile additional source files yield some progress:
$ emcc -s EXPORTED_FUNCTIONS="['_Rf_pf']" nmath/pf.c nmath/pbeta.c nmath/pchisq.c
warning: unresolved symbol: R_finite
warning: unresolved symbol: Rf_pgamma
warning: unresolved symbol: Rf_warning
warning: unresolved symbol: bratio
warning: unresolved symbol: gettext
warning: unresolved symbol: R_NaN
warning: unresolved symbol: R_NegInf
warning: unresolved symbol: R_PosInf
However, this process leads to a complex set of issues and eventually runs into obstacles:
$ emcc -s EXPORTED_FUNCTIONS="['_Rf_pf']" nmath/pf.c nmath/pbeta.c nmath/pchisq.c main/arithmetic.c
In file included from main/arithmetic.c:35:
include/Defn.h:201:3: error: SIZE_MAX is required for C99
# error SIZE_MAX is required for C99
^
include/Defn.h:639:9: error: unknown type name 'R_size_t'
extern0 R_size_t R_NSize INI_as(R_NSIZE);/* Size of cons cell heap */
^
include/Defn.h:640:9: error: unknown type name 'R_size_t'
extern0 R_size_t R_VSize INI_as(R_VSIZE);/* Size of the vector heap */
^
...
fatal error: too many errors emitted, stopping now [-ferror-limit=]
It is worth mentioning that the CPATH is configured, although it is uncertain if this method is effective in directing the compiler to the necessary headers:
(trunk)⚡ % echo $CPATH
gnuwin32/fixed/h/:/usr/local/Cellar/r/3.1.2_1/include/:/usr/local/Cellar/r/3.1.2_1/R.framework/Versions/3.1/Resources/include/:nmath/:include/:main/:include/R_ext