Skip to content

Commit 088c5cb

Browse files
committed
V0.6 native library loading support
1 parent 26e1d47 commit 088c5cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1175
-418
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ duktape-2.5.0/djgpp/
1515
*.d
1616
dzcomm/obj/djgpp/asmdef.s
1717
doc/
18+
dexport.c
19+
*.dxe
20+
jSH-cppcheck-build-dir/
21+
*.undef

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
**V0.6 native library loading support**
2+
* added support to load DXEs during runtime to extend functionality
3+
* moved comport to a loadable library
4+
* re-added KbHit()
5+
16
**V0.5 backport to musj**
27
* switched back to MuJS for DOjS compatibility
38
* added watt32 TCP/IP support

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The documentation states:
1717

1818
MIT License
1919

20-
Copyright (c) 2019-2020 Andre Seidelt <[email protected]>
20+
Copyright (c) 2019-2021 Andre Seidelt <[email protected]>
2121

2222
Permission is hereby granted, free of charge, to any person obtaining a copy
2323
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
###
2-
# Makefile for cross compiling DOjS for FreeDOS/MS-DOS
2+
# Makefile for cross compiling jSH for FreeDOS/MS-DOS
33
# All compilation was done with DJGPP 7.2.0 built from https://github.com/andrewwutw/build-djgpp
44
###
55
# enter the path to x-djgpp here
66
#DJGPP=/Users/iluvatar/tmp/djgpp/bin
77
DJGPP=/home/ilu/djgpp/bin
88

9-
MUJS=mujs-1.0.5
10-
DZCOMMDIR=dzcomm
11-
LIBDZCOMM=$(DZCOMMDIR)/lib/djgpp/libdzcom.a
12-
WATT32=watt32-2.2dev.rel.11/
13-
KUBAZIP=zip
9+
# subdirs
10+
MUJS = mujs-1.0.5
11+
DZCOMMDIR = dzcomm
12+
LIBDZCOMM = $(DZCOMMDIR)/lib/djgpp/libdzcom.a
13+
WATT32 = watt32-2.2dev.rel.11/
14+
KUBAZIP = zip
1415

15-
INCLUDES=-I$(MUJS) -I$(DZCOMMDIR)/include -I$(WATT32)/inc -I$(KUBAZIP)/src
16-
LIBS=-lmujs -lm -lemu -ldzcom -lwatt
16+
# compiler
17+
CDEF = -DGC_BEFORE_MALLOC #-DDEBUG_ENABLED #-DMEMDEBUG
18+
CFLAGS = -MMD -Wall -std=gnu99 -O2 -march=i386 -mtune=i586 -ffast-math $(INCLUDES) -fgnu89-inline -Wmissing-prototypes $(CDEF)
19+
INCLUDES = -I$(realpath $(MUJS)) -I$(realpath $(DZCOMMDIR))/include -I$(realpath $(WATT32))/inc -I$(realpath $(KUBAZIP))/src -I$(realpath .)
1720

18-
CDEF=-DGC_BEFORE_MALLOC #-DDEBUG_ENABLED #-DMEMDEBUG
19-
CFLAGS=-MMD -Wall -std=gnu99 -O2 -march=i386 -mtune=i586 -ffast-math $(INCLUDES) -fgnu89-inline -Wmissing-prototypes $(CDEF)
20-
LDFLAGS=-L$(MUJS)/build/release -L$(DZCOMMDIR)/lib/djgpp -L$(WATT32)/lib
21+
# linker
22+
LIBS = -lmujs -lm -lemu -ldzcom -lwatt
23+
LDFLAGS = -L$(MUJS)/build/release -L$(DZCOMMDIR)/lib/djgpp -L$(WATT32)/lib
2124

22-
EXE=JSH.EXE
23-
RELZIP=jsh.zip
25+
# output
26+
EXE = JSH.EXE
27+
RELZIP = jsh.zip
2428

25-
BUILDDIR=build
26-
27-
DOCDIR=doc/html
29+
# dirs/files
30+
BUILDDIR = build
31+
DOCDIR = doc/html
32+
DXE_TEMPLATE = dxetemplate.txt
33+
DXE_EXPORTS = dexport.c
2834

2935
CROSS=$(DJGPP)/i586-pc-msdosdjgpp
3036
CROSS_PLATFORM=i586-pc-msdosdjgpp-
@@ -33,21 +39,25 @@ AR=$(DJGPP)/$(CROSS_PLATFORM)ar
3339
LD=$(DJGPP)/$(CROSS_PLATFORM)ld
3440
STRIP=$(DJGPP)/$(CROSS_PLATFORM)strip
3541
RANLIB=$(DJGPP)/$(CROSS_PLATFORM)ranlib
42+
DXE3GEN = dxe3gen
43+
DXE3RES = dxe3res
3644
export
3745

3846
PARTS= \
3947
$(BUILDDIR)/zip/src/zip.o \
4048
$(BUILDDIR)/file.o \
41-
$(BUILDDIR)/comport.o \
4249
$(BUILDDIR)/funcs.o \
4350
$(BUILDDIR)/jsconio.o \
4451
$(BUILDDIR)/zipfile.o \
4552
$(BUILDDIR)/watt.o \
4653
$(BUILDDIR)/socket.o \
4754
$(BUILDDIR)/lowlevel.o \
48-
$(BUILDDIR)/jSH.o
55+
$(BUILDDIR)/jSH.o \
56+
$(BUILDDIR)/dexport.o
57+
58+
DXE_DIRS := $(wildcard *.dxelib)
4959

50-
all: init libmujs dzcomm libwatt32 $(EXE)
60+
all: init libmujs dzcomm libwatt32 $(EXE) $(DXE_DIRS)
5161

5262
libmujs: $(MUJS)/build/release/libmujs.a
5363

@@ -74,9 +84,15 @@ $(BUILDDIR)/%.o: %.c Makefile
7484
$(BUILDDIR)/zip/src/%.o: $(KUBAZIP)/src/%.c Makefile
7585
$(CC) $(CFLAGS) -c $< -o $@
7686

87+
$(DXE_DIRS):
88+
$(MAKE) -C $@
89+
90+
$(DXE_EXPORTS): dxetemplate.txt $(MUJS)/mujs.h
91+
python3 ./extract_functions.py $(DXE_TEMPLATE) $(MUJS)/mujs.h $@
92+
7793
zip: all doc
7894
rm -f $(RELZIP)
79-
zip -9 -v -r $(RELZIP) $(EXE) JC.JS JC.BAT CWSDPMI.EXE LICENSE README.md CHANGELOG.md jsboot/ scripts/ $(DOCDIR)
95+
zip -9 -v -r $(RELZIP) $(EXE) JC.JS JC.BAT CWSDPMI.EXE LICENSE README.md CHANGELOG.md jsboot/ scripts/ $(DOCDIR) *.dxe
8096

8197
doc:
8298
rm -rf $(DOCDIR)
@@ -91,9 +107,12 @@ init:
91107
clean:
92108
rm -rf $(BUILDDIR)/
93109
rm -f $(PARTS) $(EXE) $(ZIP) JSLOG.TXT
110+
for dir in $(DXE_DIRS); do \
111+
$(MAKE) -C $$dir -f Makefile $@; \
112+
done
94113

95-
distclean: clean jsclean dzclean wattclean
96-
rm -rf $(DOCDIR) TEST.TXT JSLOG.TXT
114+
distclean: clean jsclean dzclean wattclean dxeclean
115+
rm -rf $(DOCDIR) TEST.TXT JSLOG.TXT *.dxe
97116

98117
dzclean:
99118
$(MAKE) -C $(DZCOMMDIR) clean
@@ -107,10 +126,13 @@ wattclean:
107126
zclean:
108127
$(MAKE) -C $(ZLIB) -f Makefile.dojs clean
109128

129+
dxeclean:
130+
rm -f $(DXE_EXPORTS)
131+
110132
fixnewlines:
111133
find . -iname *.sh -exec dos2unix -v \{\} \;
112134

113-
.PHONY: clean distclean init doc
135+
.PHONY: clean distclean init doc $(DXE_DIRS)
114136

115137
DEPS := $(wildcard $(BUILDDIR)/*.d)
116138
ifneq ($(DEPS),)

Makefile.dxemk

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
all: $(DXE_NAME) check_exports
2+
3+
clean distclean:
4+
rm -rf *.dxe *.d *.o *.undef
5+
6+
%.o: %.c
7+
$(CC) -o $@ -c $(CFLAGS) $<
8+
9+
%.dxe: %.o
10+
$(DXE3GEN) -o $@ $< $(DXE_LDFLAGS) -U -E _init_ -E _shutdown_ -V | tee $(basename $@).undef | sort
11+
cp $@ ..
12+
13+
check_exports: $(DXE_NAME) ../$(DXE_TEMPLATE)
14+
python3 ../check_exports.py ../$(DXE_TEMPLATE) $(basename $<).undef

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ I used the following command lines to update/install my dependencies:
6060
```bash
6161
sudo apt-get update
6262
sudo apt-get dist-upgrade
63-
sudo apt-get install bison flex curl gcc g++ make texinfo zlib1g-dev g++ unzip htop screen git bash-completion build-essential npm python-yaml zip dos2unix
63+
sudo apt-get install bison flex curl gcc g++ make texinfo zlib1g-dev g++ unzip htop screen git bash-completion build-essential npm python-yaml zip dos2unix python3
6464
sudo npm install -g jsdoc
6565
sudo npm install -g better-docs
6666
```
@@ -73,6 +73,8 @@ export DJGPP_PREFIX=/home/ilu/djgpp
7373
./build-djgpp.sh 7.2.0
7474
```
7575

76+
Make sure `dxe3gen` and `dxe3res` are installed as well and set `DJPATH` permanently to your installation directory of DJGPP.
77+
7678
## Getting & Compiling jSH
7779
Open a shell/command line in the directory where you want the source to reside.
7880

@@ -86,6 +88,17 @@ Open the Makefile in a text editor and change the path to DJGPP according to you
8688
Now you are ready to compile jSH with `make clean all`. This might take some time as the dependencies are quite a large.
8789
`make distclean` will clean dependencies as well. `make zip` will create the distribution ZIP and `make doc` will re-create the HTML help.
8890

91+
## Creating native libraries (DXEs)
92+
Have a look at the example libraries in `test.dxelib/` and `test2.dxelib/`. Some rules:
93+
* A library called `foo` must have the filename `foo.dxe` and must provide at least the function `void init_foo(js_State *J)`. This function is called when the library is loaded and can then register global variables/objects/classes/function in the Javascript runtime.
94+
* The library may also provide the function `void shutdown_foo(void)` (this is completely optional). This will get called during shutdown and can perform cleanup.
95+
* All libraries in the sourcetree will be build by the `make all` target if their dir-name ends with `.dxelib`. The Makefile in the directory must provide the targets all, clean and distclean (see examples).
96+
* Libraries should use the `Makefile.dxemk` whenever possible.
97+
* In theory it should be possible to compile additional native libraries without re-compiling `jSH.EXE` if the same compiler is used (see above).
98+
* Native libraries may use all functions from `mujs.h` and all functions in `dxetemplate.txt`. If additional functions are needed these need to be included in the template and `jSH.EXE` must be recompiled!
99+
* When libraries are compiled the exports provided by jSH are checked against the used functions (see `check_exports.py`) and the build fails if there are symbols missing.
100+
* Feel free to provide your native libraries and/or changes to `dxetemplate.txt` for inclusion in the jSH GitHub repository (MIT license please)...
101+
89102
# History
90103
See the [changelog](/CHANGELOG.md) for the projects history.
91104

check_exports.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
import re
3+
4+
if len(sys.argv) < 2:
5+
print("Usage:\n {} <template> <undef file>".format(sys.argv[0]))
6+
exit(1)
7+
8+
fd = re.compile(r"unresolved: `_(\w*)'")
9+
10+
had_errors = False
11+
12+
with open(sys.argv[1], "r") as template:
13+
with open(sys.argv[2], "r") as undeffile:
14+
15+
# create a set with available exports
16+
exports = []
17+
for l in template:
18+
l = l.strip()
19+
if not l.startswith("//") and not l.startswith("#") and len(l) > 0:
20+
exports.append(l)
21+
available = set(exports)
22+
23+
# check if all symbols exist
24+
for l in undeffile:
25+
res = fd.search(l)
26+
if res:
27+
func = res.group(1)
28+
if not func.startswith("js_"):
29+
if func not in exports:
30+
had_errors = True
31+
print("WARNING: DXE export '{}' missing!".format(func))
32+
33+
# fail if there were missing symbols
34+
if had_errors:
35+
print("\n\n!!! DXE export check FAILED!\n\n")
36+
exit(1)
37+
else:
38+
print("DXE export check OK!")
39+
exit(0)

comport.dxelib/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DXE_LDFLAGS = -L../$(DZCOMMDIR)/lib/djgpp -ldzcom
2+
DXE_NAME = comport.dxe
3+
4+
include ../Makefile.dxemk

comport.c renamed to comport.dxelib/comport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2019-2020 Andre Seidelt <[email protected]>
4+
Copyright (c) 2019-2021 Andre Seidelt <[email protected]>
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

comport.h renamed to comport.dxelib/comport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2019-2020 Andre Seidelt <[email protected]>
4+
Copyright (c) 2019-2021 Andre Seidelt <[email protected]>
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

doc/classes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ File.prototype.WriteBytes = function (data) { };
5858
*/
5959
/**
6060
* Open a COM port.
61+
* Note: COM port functions must be activated/loaded by calling LoadLibrary("comport")!
62+
*
6163
* @class
6264
* @param {COM} port one of COM.PORT.x
6365
* @param {COM} baud one of COM.BAUD.x

doc/internal.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ JSH_VERSION = 0.0;
2222
* @module general
2323
*/
2424

25+
/**
26+
* Load and initialize a native library (DXE). Native libraries must reside in the directory jSH.EXE was started from!
27+
*
28+
* @param {string} name the base name of the library (e.g. if the library is called "foo.dxe" on disk you need to call LoadLibrary("foo")).
29+
*/
30+
function LoadLibrary(name) { }
31+
2532
/**
2633
* Write data to stdout.
2734
* @param {string} s the string to print.

doc/jsdoc.conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"outputSourceFiles": false
2222
},
2323
"better-docs": {
24-
"name": "jSH V0.5",
24+
"name": "jSH V0.6",
2525
"hideGenerator": true,
2626
"navLinks": [
2727
{

0 commit comments

Comments
 (0)