Skip to content

Commit 841f87e

Browse files
committed
[[ Java FFI Support ]] Add LCB Java support module
This patch adds an LCB java support module to libscript which wraps the libfoundation Java support API. It attempts to initialise the JVM when any of its functionality is used.
1 parent 3726ccb commit 841f87e

4 files changed

Lines changed: 448 additions & 0 deletions

File tree

libscript/src/java.lcb

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/* Copyright (C) 2016 LiveCode Ltd.
2+
3+
This file is part of LiveCode.
4+
5+
LiveCode is free software; you can redistribute it and/or modify it under
6+
the terms of the GNU General Public License v3 as published by the Free
7+
Software Foundation.
8+
9+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
10+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
16+
17+
/**
18+
This module provides utility handlers for converting to and from Java types.
19+
*/
20+
module com.livecode.java
21+
22+
use com.livecode.foreign
23+
24+
public foreign type JObject binds to "MCJavaObjectTypeInfo"
25+
public type JString is JObject
26+
public type JByteArray is JObject
27+
28+
foreign handler MCJavaStringFromJString(in pString as JObject, out rString as String) returns nothing binds to "<builtin>"
29+
foreign handler MCJavaStringToJString(in pString as String, out rString as JObject) returns nothing binds to "<builtin>"
30+
foreign handler MCJavaDataFromJByteArray(in pByteArray as JObject, out rData as Data) returns nothing binds to "<builtin>"
31+
foreign handler MCJavaDataToJByteArray(in pData as Data, out rByteArray as JObject) returns nothing binds to "<builtin>"
32+
33+
foreign handler MCJavaGetClassName(in pObject as JObject, out rName as String) returns nothing binds to "<builtin>"
34+
35+
36+
/**
37+
Sumary: Get Java class name of a Java object
38+
39+
Parameters:
40+
pObj: A JObject
41+
42+
Example:
43+
foreign handler CreateJavaObject() returns JObject binds to "java:java.lang.Object>new()"
44+
45+
public handler GetNewJavaObject() returns JObject
46+
variable tObj as JObject
47+
unsafe
48+
put CreateJavaObject() into tObj
49+
50+
variable tClassName as String
51+
put GetJavaClassName(pObj) into tClassName
52+
53+
-- tClassName contains "java.lang.Object"
54+
end unsafe
55+
56+
return tObj
57+
end handler
58+
59+
Description:
60+
Use <GetJavaClassName> to find out what class a given Java object is an
61+
instance of.
62+
63+
*/
64+
public handler GetJavaClassName(in pObj as JObject) returns String
65+
variable tString as String
66+
unsafe
67+
MCJavaGetClassName(pObj, tString)
68+
end unsafe
69+
return tString
70+
end handler
71+
72+
/**
73+
Sumary: Convert a java string into a String
74+
75+
Parameters:
76+
pObj: The JString to convert
77+
78+
Example:
79+
80+
foreign handler JavaGetDefaultLocale() returns JObject binds to "java:java.util.Locale>getDefault()Ljava/util/Locale;!static"
81+
foreign handler JavaLocaleDisplayName(in pLocale as JObject) returns JObject binds to "java:java.util.Locale>getDisplayName()Ljava/lang/String;"
82+
83+
public handler GetDefaultLocaleDisplayName() returns String
84+
unsafe
85+
variable tLocale as JObject
86+
put JavaGetDefaultLocale() into tLocale
87+
88+
variable tDisplay as JString
89+
put JavaLocaleDisplayName(tLocale) into tDisplay
90+
91+
return StringFromJString(tDisplay)
92+
end unsafe
93+
end handler
94+
95+
96+
Description:
97+
Use <StringFromJString> to convert an instance of the class java.lang.String
98+
to a variable of type String.
99+
100+
*/
101+
public handler StringFromJString(in pObj as JString) returns String
102+
variable tString as String
103+
unsafe
104+
MCJavaStringFromJString(pObj, tString)
105+
end unsafe
106+
return tString
107+
end handler
108+
109+
/**
110+
Sumary: Convert a String into a java string
111+
112+
Parameters:
113+
pString: The String to convert
114+
115+
Returns:
116+
A JObject of type java.lang.String
117+
118+
Example:
119+
foreign handler CreateJavaCurrencyWithCode(in pString as JString) returns JObject binds to "java:java.util.Currency>getInstance(Ljava/lang/String;)Ljava/util/Currency;!static"
120+
121+
-- Create a new Currency object
122+
variable tCurrency as JObject
123+
unsafe
124+
variable tCode as JString
125+
put StringToJString("AMD") into tCode
126+
127+
put CreateJavaCurrencyWithCode(tCode) into tCurrency
128+
end unsafe
129+
130+
Description:
131+
Use <StringToJString> to convert a variable of type String to an instance
132+
of the class java.lang.String.
133+
*/
134+
public handler StringToJString(in pString as String) returns JString
135+
variable tString as JString
136+
unsafe
137+
MCJavaStringToJString(pString, tString)
138+
end unsafe
139+
return tString
140+
end handler
141+
142+
public handler DataToJByteArray(in pData as Data) returns JByteArray
143+
variable tBytes as JByteArray
144+
unsafe
145+
MCJavaDataToJByteArray(pData, tBytes)
146+
end unsafe
147+
return tBytes
148+
end handler
149+
150+
public handler DataFromJByteArray(in pBytes as JByteArray) returns Data
151+
variable tData as Data
152+
unsafe
153+
MCJavaDataFromJByteArray(pBytes, tData)
154+
end unsafe
155+
return tData
156+
end handler
157+
158+
end module

libscript/src/module-java.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* Copyright (C) 2003-2015 LiveCode Ltd.
2+
3+
This file is part of LiveCode.
4+
5+
LiveCode is free software; you can redistribute it and/or modify it under
6+
the terms of the GNU General Public License v3 as published by the Free
7+
Software Foundation.
8+
9+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
10+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
16+
17+
#include <foundation.h>
18+
#include <foundation-auto.h>
19+
20+
static bool TryToInitializeJava()
21+
{
22+
if (!MCJavaInitialize())
23+
return MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason",
24+
MCSTR("could not initialize java"),
25+
nullptr);
26+
27+
return true;
28+
}
29+
30+
extern "C" MC_DLLEXPORT_DEF MCTypeInfoRef MCJavaObjectTypeInfo() { return MCJavaGetObjectTypeInfo(); }
31+
32+
////////////////////////////////////////////////////////////////////////////////////////////////////
33+
34+
extern "C" MC_DLLEXPORT_DEF void MCJavaStringFromJString(MCJavaObjectRef p_object, MCStringRef &r_string)
35+
{
36+
if (!TryToInitializeJava())
37+
return;
38+
39+
if (!MCJavaConvertJStringToStringRef(p_object, r_string))
40+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason",
41+
MCSTR("couldn't convert java object to string"),
42+
nullptr);
43+
}
44+
45+
extern "C" MC_DLLEXPORT_DEF void MCJavaStringToJString(MCStringRef p_string, MCJavaObjectRef &r_object)
46+
{
47+
if (!TryToInitializeJava())
48+
return;
49+
50+
if (!MCJavaConvertStringRefToJString(p_string, r_object))
51+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason",
52+
MCSTR("couldn't convert string to java object"),
53+
nullptr);
54+
}
55+
56+
extern "C" MC_DLLEXPORT_DEF void MCJavaDataFromJByteArray(MCJavaObjectRef p_object, MCDataRef &r_data)
57+
{
58+
if (!TryToInitializeJava())
59+
return;
60+
61+
if (!MCJavaConvertJByteArrayToDataRef(p_object, r_data))
62+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason",
63+
MCSTR("couldn't convert java object to data"),
64+
nullptr);
65+
}
66+
67+
extern "C" MC_DLLEXPORT_DEF void MCJavaDataToJByteArray(MCDataRef p_data, MCJavaObjectRef &r_object)
68+
{
69+
if (!TryToInitializeJava())
70+
return;
71+
72+
if (!MCJavaConvertDataRefToJByteArray(p_data, r_object))
73+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason",
74+
MCSTR("couldn't convert data to java object"),
75+
nullptr);
76+
}
77+
78+
extern "C" MC_DLLEXPORT_DEF void MCJavaGetClassName(MCJavaObjectRef p_obj, MCStringRef &r_string)
79+
{
80+
if (!TryToInitializeJava())
81+
return;
82+
83+
if (!MCJavaGetJObjectClassName(p_obj, r_string))
84+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason",
85+
MCSTR("couldn't get java object class name"),
86+
nullptr);
87+
}
88+
89+
////////////////////////////////////////////////////////////////////////////////////////////////////
90+
91+
extern "C" bool com_livecode_java_Initialize(void)
92+
{
93+
if (!MCJavaCreateJavaObjectTypeInfo())
94+
return false;
95+
96+
return true;
97+
}
98+
99+
extern "C" void com_livecode_java_Finalize(void)
100+
{
101+
MCJavaFinalize();
102+
}
103+
104+
////////////////////////////////////////////////////////////////////////////////////////////////////
105+

libscript/stdscript-sources.gypi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'src/file.lcb',
1818
'src/foreign.lcb',
1919
#'src/item.lcb',
20+
'src/java.lcb',
2021
#'src/line.lcb',
2122
'src/list.lcb',
2223
'src/logic.lcb',
@@ -53,6 +54,7 @@
5354
'src/module-encoding.cpp',
5455
'src/module-file.cpp',
5556
'src/module-foreign.cpp',
57+
'src/module-java.cpp',
5658
'src/module-list.cpp',
5759
'src/module-logic.cpp',
5860
'src/module-map.cpp',

0 commit comments

Comments
 (0)