add tcl+java example

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8140 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-30 11:59:54 +00:00
commit b4e4fe23ca
10 changed files with 144 additions and 13 deletions

View file

@ -3,24 +3,31 @@ Version 1.3.28 (unreleased).
12/30/2005: mmatus
- Add initial support for gcj and Java -> Python. See
Examples/python/java
- Add initial support for gcj and Java -> <target language> mechanism.
to see how to use gcj and how to export java classes into
python.
See examples in:
Examples/python/java
Examples/ruby/java
Examples/tcl/java
to see how to use gcj+swig to export java classes into
python/ruby/tcl.
The idea is to put all the common code for gcj inside
Lib/gcj
such that it can be used from other languages.
Python provide a simple jstring.i interface, which can be
used as a modelo to ruby,tcl and perl, ie, the languages
that use the UTL.
and localize especific issues as jstring, as can be found
in
Lib/python/jstring.i
Lib/ruby/jstring.i
Lib/tcl/jstring.i
Using the UTL, this is very easy, and the perl version for
jstring.i will be next.
12/29/2005: mmatus
- Add the copyctor feature/directive to enable the automatic

View file

@ -13,3 +13,4 @@ reference
simple
value
variables
java

View file

@ -0,0 +1,29 @@
public class Example {
public int mPublicInt;
public Example() {
mPublicInt = 0;
}
public Example(int IntVal) {
mPublicInt = IntVal;
}
public int Add(int a, int b) {
return (a+b);
}
public float Add(float a, float b) {
return (a+b);
}
public String Add(String a, String b) {
return (a+b);
}
public Example Add(Example a, Example b) {
return new Example(a.mPublicInt + b.mPublicInt);
}
}

View file

@ -0,0 +1,28 @@
CXX = g++
TOP = ../..
SWIG = $(TOP)/../preinst-swig
CXXSRCS =
TARGET = example
INTERFACE = example.i
LIBS = -lm
all:: Example.class
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' CXX="gcj" CFLAGS="-fPIC" \
TCL_CXXSHARED="gcj -fPIC -shared Example.class -lstdc++ " DEFS='' tcl_cpp
clean::
$(MAKE) -f $(TOP)/Makefile tcl_clean
rm -f *.class Example.h
check: all
run:
tclsh runme.tcl
Example.class: Example.java
gcj -v || exit 0
gcj -fPIC -C -c -g Example.java
gcjh Example

View file

@ -0,0 +1,10 @@
%module example
%include <gcj/cni.i>
%include <jstring.i>
%{
#include "Example.h"
%}
%include Example.h

View file

@ -0,0 +1,15 @@
catch { load ./example[info sharedlibextension] example}
JvCreateJavaVM NULL
JvAttachCurrentThread NULL NULL
Example e1 1
Example e2 2
puts "[e1 cget -mPublicInt]"
puts "[e2 cget -mPublicInt]"
puts "[e2 Add 1 2]"
puts "[e2 Add 1.0 2.0]"
puts "[e2 Add '1' '2']"
JvDetachCurrentThread

View file

@ -26,8 +26,7 @@ SWIG_From(jstring)(jstring val)
{
if (!val) {
return Qnil;
}
else {
} else {
jint len = JvGetStringUTFLength(val);
char buf[len];
JvGetStringUTFRegion(val, 0, len, buf);

42
Lib/tcl/jstring.i Normal file
View file

@ -0,0 +1,42 @@
%include <typemaps/valtypes.swg>
%fragment(SWIG_AsVal_frag(jstring),"header") {
SWIGINTERN int
SWIG_AsVal_dec(jstring)(Tcl_Obj * obj, jstring *val)
{
int len = 0;
const char *cstr = Tcl_GetStringFromObj(obj, &len);
if (!cstr || (strcmp(cstr,"NULL") == 0)) {
if (val) *val = 0;
return SWIG_OK;
} else {
int len = 0;
const Tcl_UniChar *ucstr = Tcl_GetUnicodeFromObj(obj,&len);
if (val) {
*val = JvNewString((const jchar*)ucstr, len);
}
}
return SWIG_NEWOBJ;
}
}
%fragment(SWIG_From_frag(jstring),"header") {
SWIGINTERNINLINE Tcl_Obj *
SWIG_From_dec(jstring)(jstring val)
{
if (!val) {
return Tcl_NewStringObj("NULL",-1);
} else {
return Tcl_NewUnicodeObj((Tcl_UniChar *)JvGetStringChars(val),JvGetStringUTFLength(val));
}
}
}
%typemaps_asvalfrom(%checkcode(STRING),
%arg(SWIG_AsVal(jstring)),
%arg(SWIG_From(jstring)),
%arg(SWIG_AsVal_frag(jstring)),
%arg(SWIG_From_frag(jstring)),
java::lang::String *);