Sunday, September 9, 2018

DJGPP: Duktape

Duktape is and embeddable JS lib for C/C++. I tried cross compiling it for DJGPP and it works, making it possible for you to run JS in DOS.

Get the source at https://github.com/svaarala/duktape, extract the source, and compile as follows:

i586-pc-msdosdjgpp-gcc -c \
-o duktape.o \
-Isrc/ src/duktape.c \
-DDUK_USE_DATE_FMT_STRFTIME \
-DDUK_USE_DATE_PRS_STRPTIME

Then archive as follows
 
i586-pc-msdosdjgpp-ar ar rvs \
libduk.a \
duktape.o

By default you will find that the library does not come with console.log(), so to get this to work you can compile the static library as follows:
 
i586-pc-msdosdjgpp-gcc -c \
-o duk_cmdline.o \
-Isrc/ ./src/duk_cmdline.c \
-DDUK_CMDLINE_CONSOLE_SUPPORT \
-DDUK_USE_DATE_PRS_STRFTIME \
-DDUK_USE_DATE_FMT_STRFTIME \
-I./include 
 
i586-pc-msdosdjgpp-gcc -c \
-o duk_console.o \
-Isrc/ ./src/duk_console.c \
-DDUK_CMDLINE_CONSOLE_SUPPORT \
-DDUK_USE_DATE_PRS_STRFTIME \
-DDUK_USE_DATE_FMT_STRFTIME \
-I./include

And archive as follows
 
i586-pc-msdosdjgpp-ar ar rvs \
libduktape.a \
duktape.o \
duk_cmdline \
duk_console.o

You can then copy the archive to your DJGPP cross compiler directory . Take note that if you want to use g++, you need to compile with i586-pc-msdosdjgpp-g++ In order to test out the library, you can compile the Duk REPL located in examples/cmdline as follows
 
i586-pc-msdosdjgpp-gcc -W -Wno-unused-function \
-I./include \
-I/home/skullquake/hosts/dos/usr/include \
-L./lib \
-L/home/skullquake/hosts/dos/usr/lib \
-DDUK_CMDLINE_CONSOLE_SUPPORT \
-DDUK_USE_DATE_FMT_STRFTIME \
./examples/cmdline/duk_cmdline.c \
-lduktape \
-lm \
-o ./duk

You can test it out in DOSBOX via the REPL or by passing in [multiple] script files.





I've tested with libraries (e.g. Sylvester, BioNode), and it works...most of the time




Duktape focuses on portability and footprint. Not all JS libs you try out will work, but many will.

You can register C functions and variables with Duktape, allowing executing native functions from JS function calls. I'm currently working on creating bindings for Allegro which allows you to easily code graphics stuff in Allegro without having to recompile the whole program, just reading everything from a JS file.