furthur cleanup of js

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@728 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2006-03-02 15:22:39 +00:00
parent 55b7373a07
commit 0b22a15338

View File

@ -1513,141 +1513,71 @@ static int env_init(JSContext *cx, JSObject *javascript_object)
return 1; return 1;
} }
static void js_exec(switch_core_session *session, char *data) static void js_parse_and_execute(switch_core_session *session, char *input_code)
{ {
char *code, *next, *arg, *nextarg; JSObject *javascript_global_object = NULL, *session_obj = NULL;
int res=-1; char buf[1024], *script, *arg, *argv[512];
jsval rval; int argc = 0, x = 0, y = 0;
JSContext *cx; unsigned int flags = 0;
JSObject *javascript_global_object, *session_obj;
struct js_session jss; struct js_session jss;
int x = 0, y = 0; JSContext *cx = NULL;
char buf[512]; jsval rval;
int flags = 0;
switch_channel *channel;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
if (!data) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "js requires an argument (filename|code)\n");
return;
}
code = switch_core_session_strdup(session,(char *)data);
if (code[0] == '-') {
if ((next = strchr(code, '|'))) {
*next = '\0';
next++;
}
code++;
code = next;
}
if (!code) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "js requires an argument (filename|code)\n");
return;
}
if ((cx = JS_NewContext(globals.rt, globals.gStackChunkSize))) { if ((cx = JS_NewContext(globals.rt, globals.gStackChunkSize))) {
JS_SetErrorReporter(cx, js_error); JS_SetErrorReporter(cx, js_error);
if ((javascript_global_object = JS_NewObject(cx, &global_class, NULL, NULL)) && javascript_global_object = JS_NewObject(cx, &global_class, NULL, NULL);
env_init(cx, javascript_global_object) && env_init(cx, javascript_global_object);
(session_obj = new_js_session(cx, javascript_global_object, session, &jss, "session", flags))) { JS_SetGlobalObject(cx, javascript_global_object);
JS_SetGlobalObject(cx, javascript_global_object);
JS_SetPrivate(cx, javascript_global_object, session);
res = 0;
/* Emaculent conception of session object into the script if one is available */
do { if (session && (session_obj = new_js_session(cx, javascript_global_object, session, &jss, "session", flags))) {
if ((next = strchr(code, '|'))) { JS_SetPrivate(cx, javascript_global_object, session);
*next = '\0';
next++;
}
if ((arg = strchr(code, ':'))) {
for (y=0;(arg=strchr(arg, ':'));y++)
arg++;
arg = strchr(code, ':');
*arg = '\0';
arg++;
snprintf(buf, sizeof(buf), "~var Argv = new Array(%d);", y);
eval_some_js(buf, cx, javascript_global_object, &rval);
snprintf(buf, sizeof(buf), "~var argc = %d", y);
eval_some_js(buf, cx, javascript_global_object, &rval);
do {
if ((nextarg = strchr(arg, ':'))) {
*nextarg = '\0';
nextarg++;
}
snprintf(buf, sizeof(buf), "~Argv[%d] = \"%s\";", x++, arg);
eval_some_js(buf, cx, javascript_global_object, &rval);
arg = nextarg;
} while (arg);
}
if (!(res=eval_some_js(code, cx, javascript_global_object, &rval))) {
break;
}
code = next;
} while (code);
} }
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Allocation Error!\n");
return;
}
script = input_code;
if ((arg = strchr(script, ' '))) {
*arg++ = '\0';
argc = switch_separate_string(arg, ':', argv, (sizeof(argv) / sizeof(argv[0])));
} }
if (argc) { /* create a js doppleganger of this argc/argv*/
snprintf(buf, sizeof(buf), "~var Argv = new Array(%d);", argc);
eval_some_js(buf, cx, javascript_global_object, &rval);
snprintf(buf, sizeof(buf), "~var argc = %d", argc);
eval_some_js(buf, cx, javascript_global_object, &rval);
for (y = 0; y < argc; y++) {
snprintf(buf, sizeof(buf), "~Argv[%d] = \"%s\";", x++, argv[y]);
eval_some_js(buf, cx, javascript_global_object, &rval);
}
}
if (cx) { if (cx) {
eval_some_js(script, cx, javascript_global_object, &rval);
JS_DestroyContext(cx); JS_DestroyContext(cx);
} }
} }
static void *SWITCH_THREAD_FUNC js_thread_run(switch_thread *thread, void *obj) static void *SWITCH_THREAD_FUNC js_thread_run(switch_thread *thread, void *obj)
{ {
JSContext *cx = NULL;
JSObject *javascript_global_object = NULL;
char buf[1024];
char *code, *next, *arg, *nextarg;
jsval rval;
int x = 0;
char *input_code = obj; char *input_code = obj;
cx = JS_NewContext(globals.rt, globals.gStackChunkSize);
javascript_global_object = JS_NewObject(cx, &global_class, NULL, NULL); js_parse_and_execute(NULL, input_code);
env_init(cx, javascript_global_object);
code = input_code;
do {
if ((next = strchr(code, '|'))) {
*next = '\0';
next++;
}
if ((arg = strchr(code, ':'))) {
int y;
for (y=0;(arg=strchr(arg, ':'));y++)
arg++;
arg = strchr(code, ':');
*arg = '\0';
arg++;
snprintf(buf, sizeof(buf), "~var Argv = new Array(%d);", y);
eval_some_js(buf, cx, javascript_global_object, &rval);
snprintf(buf, sizeof(buf), "~var argc = %d", y);
eval_some_js(buf, cx, javascript_global_object, &rval);
do {
if ((nextarg = strchr(arg, ':'))) {
*nextarg = '\0';
nextarg++;
}
snprintf(buf, sizeof(buf), "~Argv[%d] = \"%s\";", x++, arg);
eval_some_js(buf, cx, javascript_global_object, &rval);
arg = nextarg;
} while (arg);
}
if (!eval_some_js(code, cx, javascript_global_object, &rval)) {
break;
}
code = next;
} while (code);
if (input_code) { if (input_code) {
free(input_code); free(input_code);
} }
return NULL; return NULL;
} }
@ -1683,7 +1613,7 @@ static switch_status launch_async(char *text, char *out, size_t outlen)
static const switch_application_interface ivrtest_application_interface = { static const switch_application_interface ivrtest_application_interface = {
/*.interface_name */ "javascript", /*.interface_name */ "javascript",
/*.application_function */ js_exec, /*.application_function */ js_parse_and_execute,
NULL, NULL, NULL, NULL, NULL, NULL,
/*.next*/ NULL /*.next*/ NULL
}; };