/* getting code for this js_code we use call to get method to open way for future changes
ie. we do not call select from database directly intentionaly */
err=0;
if (par_id != -2) {
@co_getProp(par_area, '"js_code"',par_id,'"code"');
}
else {
get_str="";
par_id=0;
}
if(err==0)
{
js_code = get_str;
}
else
{
cou_sendMessage(cnx, ap_psprintf(p, "could not find code for id: %d",par_id));
return -1;
};
cou_sendMessage(cnx, "target:");
cou_sendMessage(cnx, target_name);
cou_sendMessage(cnx, "method:");
cou_sendMessage(cnx, method_name);
cox_initJSContext( cnx, false ); /* second parameter is discarded (false used) */
/*
* now, we have to take care of the context and make sure it is destroyed
* in any case, including errors (cou_error) in any function called,
* in JavaScript, in native methods
*/
sigenv_tmp[0] = cnx->sigenv[0];
if ( sigsetjmp(cnx->sigenv, CO_ERR_SAVE_SIGNALS) == 0) {
jscx = cnx->jscnx;
cjs_setCOInParams(jscx, in_params);
cjs_setCOMethodName(jscx, method_name);
cjs_setCOTargetName(jscx, target_name);
globalObj = JS_GetGlobalObject(jscx);
jsok = JS_EvaluateScript(jscx, globalObj, prolog, strlen(prolog),
"prolog", 1, &rval);
if (!jsok) {
cjs_dumpErrorMessages(jscx);
cou_error( cnx, "js_code::evaluate: JS_ExecuteScript failed.");
}
/* now, if we got asp, it is time to switch line translation */
if (asp!=0) {
/* asp line translation from ap_parser.h */
cjs_setCOLineTranslation(jscx, loc_lineTranslation );
}
if (direct_code) function_source = direct_code;
else function_source = js_code;
cou_sendMessage(cnx, "f source:");
cou_sendMessage(cnx, function_source);
/* set uses to library list */
if (direct_libs) {
uses = direct_libs;
}
else {
/* is there //#uses: at the beginning of code? */
if( (!strncmp(function_source, uses_pat_begin, strlen(uses_pat_begin))) ) {
uses = function_source + strlen(uses_pat_begin);
cou_sendMessage(cnx, "uses 1: ");
cou_sendMessage(cnx, uses);
} /* or inside at a beginning of a line? */
else if( (uses=strstr(function_source, uses_pat)) ) {
uses += strlen(uses_pat);
}
}
if(uses)
{
// function_beg_source = ap_pnstrdup(p,function_source, uses-function_source);
// function_source = uses;
uses = ap_getword_nulls_nc(r->pool, (char **)&uses, ';');
// function_source += strlen(uses)+ strlen(uses_pat);
@co_lcall(par_area,'"catalog_type"',getIdForClasses,par_area=>par_area,par_class=>'"catalog_type"',parent_class=>'"js_lib"',class_name=>par_class,par_id=>'"-1"');
__GET_RETURNED_INTEGER(id_catalog_type)
cou_sendMessage(cnx, "uses: ");
cou_sendMessage(cnx, uses);
i=0;
while( (lib_name = ap_getword(r->pool, &uses, ',')) )
{
cou_sendMessage(cnx, "lib_name: ");
cou_sendMessage(cnx, lib_name);
lib_name = cou_spaceTrim(cnx, lib_name);
@co_filterByProp(par_area, '"js_lib"','"id"','"name"',lib_name);
nTuples = cor_GetTupleCount(cnx, corec);
for(i=0; i<nTuples; i++)
{
cou_sendMessage(cnx, "poszczegolny lib_id: ");
cou_sendMessage(cnx, cor_GetValue(cnx, corec,i,0));
id_lib = atoi(cor_GetValue(cnx, corec,i,0));
/* getting library code */
if(id_lib>0)
{
@co_getProp(par_area, '"js_lib"', id_lib,'"id_catalog_type"');
if (id_catalog_type == atoi(get_str)) {
@co_getProp(par_area, '"js_lib"', id_lib,'"id_item"');
id_js_lib_i = atoi(get_str);/* id_js_code */
if (id_js_lib_i>0) /* has code attached */
{
@co_getProp(par_area, par_class, id_js_lib_i, '"code"');
cou_logError(cnx, CO_DEBUG, "Evaluating: %s", get_str);
jsok = JS_EvaluateScript(jscx, globalObj, get_str, strlen(get_str),
ap_psprintf(p,"library %s[%d]", lib_name, id_js_lib_i), 1, &rval);
if (!jsok) {
cou_error( cnx, "js_code::evaluate: JS_ExecuteScript failed.");
}
}
}
@co_lcall(par_area,'"js_lib"',getTree,par_area=>par_area,par_class=>'"js_lib"',par_id=>id_lib,prop_name=>'"id_item"',deli=>'","',group_deli=>empty,callback=>empty);
ids_libs = (char *) cou_getReturnValue(cnx, CO_STRING, &err);
j=0;
while( (id_js_lib = ap_getword(r->pool, &ids_libs, ',')) )
{
if(strlen(id_js_lib)>0)
{
id_js_lib_i = atoi(id_js_lib);
cou_sendMessage(cnx, ap_psprintf(r->pool,"id: %d",id_js_lib_i));
@co_getProp(par_area, par_class, id_js_lib_i, '"code"');
// snprintf(JSOutBuf+strlen(JSOutBuf),JSBUFSIZE-strlen(JSOutBuf), "%s;", get_str);
jsok = JS_EvaluateScript(jscx, globalObj, get_str, strlen(get_str),
ap_psprintf(p,"library %s[%d]", lib_name, id_js_lib_i), 1, &rval);
if (!jsok) {
cou_error( cnx, "js_code::evaluate: JS_ExecuteScript failed.");
}
};
if(strlen(ids_libs)==0) break;
if(j++ >5) break;
}; // while id_js_lib
}; // if id_lib>0
}; // for
if(strlen(uses)==0) break;
if(i++ >5) break;
};//while lib_name
}; // if uses
// snprintf(JSOutBuf+strlen(JSOutBuf),JSBUFSIZE-strlen(JSOutBuf), "function __temporary_%d() {%s}", par_id,function_source);
/* Wrap code so that variables are local */
/* Do not change line numbers - unless you want to write
your own line translation function! */
/* close function after new line - the last line of code could be // comment */
snprintf(JSOutBuf,JSBUFSIZE, "function __temporary_%d() {%s\n}", par_id, function_source);
cou_sendMessage(cnx, "source:");
cou_sendMessage(cnx, JSOutBuf);
if (asp==0) {
jsok = JS_EvaluateScript(jscx, globalObj, JSOutBuf, strlen(JSOutBuf),
"code", 1, &rval);
}
else {
/* we got asp in direct_code */
jsok = JS_EvaluateScript(jscx, globalObj, JSOutBuf, strlen(JSOutBuf),
"asp", 1, &rval);
}
if (!jsok) {
cou_error( cnx, "js_code::evaluate: JS_ExecuteScript failed.");
}
old_send_data = cnx->send_data;
cnx->send_data = 0;
/* JS takes care of cou_error calls from native methods */
jsok = JS_CallFunctionName(jscx, globalObj, ap_psprintf(p,"__temporary_%d",par_id), 0, NULL, &rval);
cnx->send_data = old_send_data;
if (!jsok) {
cou_error( cnx, "js_code::evaluate: JS_CallFunctionName failed.");
}
cou_setReturnValue( cnx,
(void*)ap_pstrdup(p, JS_GetStringBytes(JS_ValueToString(jscx, rval))), CO_STRING);
}
else {
/* error */
/* restore error context */
cnx->sigenv[0] = sigenv_tmp[0];
cjs_dumpErrorMessages(jscx);
JS_DestroyContext(jscx); /* destroy _current_ JavaScript context */
cou_error( cnx, "js_code::evaluate: cou_error called.");
}
/* was OK */
JS_DestroyContext(jscx); /* destroy _current_ context */
/* OK, no context */
expression_result = (char*)cou_getReturnValue( cnx, CO_STRING, &err );
cou_logError( cnx, CO_LOG_DEBUG, "Result: %s", expression_result );
cou_logError( cnx, CO_LOG_DEBUG, "Error: %d", err );
/* restore error context */
cnx->sigenv[0] = sigenv_tmp[0];