1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/*
* Java binding for Entity
*
* Author: Doug McBride <dougm@liberate.com>
*/
/*
* This renderer is free software; please see the LICENSE file for
* specific information as to licensing conditions.
*/
/*
* $Source: /home/cvs/entity/renderers/java/java-embed.c,v $
* $Id: java-embed.c,v 1.3 2001/05/09 00:20:04 giantpez Exp $
*/
#include <stdlib.h>
#include <jni.h>
#include "entity.h"
#include "java-embed.h"
#include "jni-macros.h"
/* TODO this doesn't work yet
* #include "java-ENode.h"
*/
/* TODO (add WIN32 support, they use ";") */
#define PATH_SEPARATOR ":"
#define MAX_PATH_LEN 1024
typedef JNIEnv *JNIEnvPtr;
/* use JNI to create a new entity.JavaInterpreter object */
static JNIEnvPtr
java_create_interp ()
{
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vmArgs;
jclass classId;
jmethodID methodId;
JavaVMOption *options;
char *cpOption;
char *libOption;
char classpath[1024] = ".";
const char *userClasspath;
const char *userLibPath;
/* this will hold our options */
options = g_malloc0(2 * sizeof(JavaVMOption));
memset(&vmArgs, 0, sizeof(vmArgs));
vmArgs.version = JNI_VERSION_1_2;
vmArgs.nOptions = 0; /* so far */
vmArgs.options = options;
vmArgs.ignoreUnrecognized = JNI_FALSE;
/* determine classpath */
userClasspath = getenv("CLASSPATH");
if (userClasspath)
strcpy(classpath, userClasspath);
EDEBUG (("java", "classpath is '%s'", classpath));
/* XXX 40 is paranoid */
cpOption = g_malloc0(strlen(classpath) + 40);
sprintf(cpOption, "-Djava.class.path=%s", classpath);
options[vmArgs.nOptions].optionString = cpOption;
options[vmArgs.nOptions].extraInfo = NULL;
++vmArgs.nOptions;
/* determine bin library path for dynamic loading */
userLibPath = getenv("LD_LIBRARY_PATH");
if (userLibPath)
{
EDEBUG (("java", "libpath is '%s'", userLibPath));
/* XXX 40 is paranoid */
libOption = g_malloc0(strlen(userLibPath) + 40);
sprintf(libOption, "-Djava.library.path=%s", userLibPath);
options[vmArgs.nOptions].optionString = libOption;
options[vmArgs.nOptions].extraInfo = NULL;
++vmArgs.nOptions;
}
if (1) /* FIXME there's probably a real debugging flag */
{
int i = 0;
EDEBUG (("java", "JavaVM args:\n "));
EDEBUG (("java", "version 0x%08lx, ", vmArgs.version));
EDEBUG (("java", "ignoreUnrecognized is %s, ",
vmArgs.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE"));
EDEBUG (("java", "nOptions is %ld\n", vmArgs.nOptions));
for (i = 0; i < vmArgs.nOptions; i++)
EDEBUG (("java", " option[%2d] = '%s'\n",
i, vmArgs.options[i].optionString));
}
if (JNI_CreateJavaVM(&jvm, (void*) &env, &vmArgs) != JNI_OK)
{
g_warning ("java: Error creating JVM [JNI_CreateJavaVM()]");
return (NULL);
}
if (JNI_GetDefaultJavaVMInitArgs((void*) &vmArgs) != JNI_OK)
{
g_warning ("java: Error creating JVM [JNI_GetDefaultJavaVMInitArgs()]");
return (NULL);
}
/* TODO don't hard-code strings like this */
EJNI_NULLBAD(NULL,classId, FindClass, "entity/JavaInterpreter");
/* we want a pointer to the "create" method. */
EJNI_NULLBAD(NULL,methodId, GetStaticMethodID, classId, "create", "()V");
/* now call the method. it returns a java.lang.String
which is NULL if it suceeds and contains an error messge
if it fails. */
(*env)->CallStaticVoidMethod(env, classId, methodId);
return env;
}
static ENode *
java_find_containing_object (ENode * node)
{
if (ebuf_equal_str (node->element, "object"))
return node;
else
return enode_parent (node, "object");
}
static char *
java_evaluate (JNIEnvPtr env, const char *expression)
{
jclass classId;
jmethodID methodID;
jstring evalText;
jstring jRetString;
char *retval = NULL;
EJNI_NULLBAD(NULL,classId, FindClass, "entity/JavaInterpreter");
EJNI_NULLBAD(NULL,evalText, NewStringUTF, expression);
/* we want a pointer to the "eval" method. */
EJNI_NULLBAD(NULL, methodID, GetStaticMethodID, classId,
"eval", "(Ljava/lang/String;)Ljava/lang/String;");
EJNI(NULL, jRetString, CallStaticObjectMethod, classId, methodID, evalText);
EDEBUG(("java", "jretstring is %i", (int) jRetString));
if (jRetString)
{
EJNI_NULLBAD(NULL, retval, GetStringUTFChars, jRetString, NULL);
EDEBUG(("java", "JavaInterpreter.eval(): '%s'", retval));
}
else
{
EDEBUG(("java", "JavaInterpreter.eval(): null", retval));
}
return retval;
}
static void
java_node_render (ENode * node)
{
ENode *containing_object = java_find_containing_object (node);
EBufConst *data = enode_get_data (node);
JNIEnvPtr interp;
if (containing_object) {
interp = (JNIEnvPtr) enode_get_kv (containing_object, "java-interp");
} else {
g_warning ("<java> tags must go within <object>'s");
return;
}
/* Save reference node on stack. Any functions or methods called dealing
* * with enode lookups will use this as the reference. */
enode_call_reference_push (node);
EDEBUG (("java", "rendering"));
/* Create our interpreter if we don't have one already. */
if (interp == NULL) {
interp = java_create_interp ();
if (!interp)
return;
enode_set_kv (containing_object, "java-interp", interp);
}
if (ebuf_not_empty (data)) {
/* XXX not handling return for now
* int result;
* */
EDEBUG (("java-embed", "executing java"));
java_evaluate (interp, data->str);
/* XXX not handling a return right now
if (!result)
{
g_warning
// TODO add the jvm error to this message
/ ("java: error evaluating code in node %s.%s: %s",
/ node->element->str, enode_attrib_str (node, "name", NULL),
/ java_error_message (interp));
//
("java: error evaluating code in node %s.%s: unknown error",
node->element->str, enode_attrib_str (node, "name", NULL));
}
*/
}
enode_call_reference_pop ();
return;
}
static void
java_node_destroy (ENode * node)
{
EDEBUG (("java", "destroying"));
/* Really ought to destroy the interpreters here. */
/* Actually, what I'd probably recommend here, is attaching * a destroy
* watcher to the containing object when you set * up the interpreter,
* and use that callback to destroy it. * That way you don't have to
* worry about multiple <java> * sections and the semantics
* involved there. */
return;
}
static EBuf *
java_execute_function (ENode * node, gchar * function, GSList * args)
{
GSList *tmp;
LangArg *arg;
gint n_args;
ENode *containing_object = java_find_containing_object (node);
JNIEnvPtr interp = (JNIEnvPtr) enode_get_kv (containing_object, "java-interp");
gint i = 0;
gint j = 0;
char **stringArgs;
char functionString[2 * 1024];
char *currentChar;
if (interp == NULL) {
g_warning
("java function '%s' asked to be executed, but no interpreter has been created for this object.",
function);
return (NULL);
}
n_args = g_slist_length (args);
/* stringArgs is an array of pointers to strings */
stringArgs = g_malloc0 (n_args * sizeof(char*));
for (tmp = args; tmp; tmp = tmp->next) {
arg = (LangArg *) tmp->data;
if (arg->type == LANG_NODE) {
/* TODO add real enode type handling */
stringArgs[i] = g_malloc0(8 * sizeof(char));
strcpy(stringArgs[i], "\"ENODE\"");
} else if (arg->type == LANG_STRING) {
char *str = arg->data;
/* FIXME we're just sticking double quotes around it for now */
/* 3 == 2 quotes plus the \0 at the end */
stringArgs[i] = g_malloc0((strlen(str) + 3) * sizeof(char));
*(stringArgs[i]) = '"';
strcpy(stringArgs[i] + 1, str);
strcpy(stringArgs[i] + strlen(str) + 1, "\"");
} else if (arg->type == LANG_INT) {
/* stringArgs[i] = g_malloc0((strlen(str) + 3) * sizeof(char)); */
sprintf(stringArgs[i], "%i", arg->intdata);
}
/* TODO support other types
} else if (arg->type == LANG_BINSTRING) {
char *str = arg->data;
int len = arg->size;
js_vm_make_string (interp->vm, &js_args[i], str, len);
js_args[i].type = JAVA_STRING;
} else if (arg->type == LANG_DOUBLE) {
js_args[i].type = JAVA_FLOAT;
js_args[i].u.vfloat = arg->doubledata;
}
*/
EDEBUG (("java", " added arg %i: '%s'", i, stringArgs[i]));
i++;
enode_call_free_arg (arg);
}
EDEBUG (("java", "calling function '%s'", function));
/*
if (!interp->vm->consts) {
g_print ("interp->vm->globals is NULL\n");
}
*/
currentChar = functionString;
/* now we build the string that gets evaluated */
/* function name */
currentChar += sprintf(functionString, "%s(", function);
/* args */
for (j = 0; j < i; ++j)
{
currentChar += sprintf(currentChar, "%s", stringArgs[j]);
if (j < i - 1)
*(currentChar++) = ',';
}
/* end */
strcpy(currentChar, ");");
/* TODO add return value handling / error checking */
EDEBUG (("java", " final call: '%s'", functionString));
java_evaluate(interp, functionString);
EDEBUG (("java", "call complete", function));
/* TODO need to free the memory allocated in this function! */
return NULL;
}
void
/* TODO support more than one option here
* #ifdef STATIC_JAVA
* java_init (RendererFlags flags)
* #else
*
*/
renderer_init (RendererFlags flags)
/* TODO support more than one option here
* #endif
*
*/
{
Element *element;
if (flags & RENDERER_REGISTER) {
/* Register java as a tag type */
element = g_malloc0 (sizeof (Element));
element->render_func = java_node_render;
element->destroy_func = java_node_destroy;
element->description = "Embed Java in your application.";
element->tag = "java";
element_register (element);
/* Register java language type */
language_register ("java", java_execute_function);
}
}
|