/*
**    File: fw_eval.js
**    Created by: Zolotarev Anton (http://sourceforge.net/projects/ajaxos)
**
**    License Information:
**    -------------------------------------------------------------------------
**    Copyright (C) 2005 Zolotarev Anton
**
**    This program is free software; you can redistribute it and/or modify it
**    under the terms of the GNU General Public License as published by the
**    Free Software Foundation; either version 2 of the License, or (at your
**    option) any later version.
**    
**    This program is distributed in the hope that it will be useful, but
**    WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**    General Public License for more details.
**    
**    You should have received a copy of the GNU General Public License along
**    with this program; if not, write to the Free Software Foundation, Inc.,
**    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

if(!fw.eval)
fw.eval = function(){
    var out = {};
    for(var i in fw.eval) out[i] = (typeof fw.eval[i]);
    out['version'] = "v0.10";
    return out;
}

fw.eval.evalNative = function(fname){
    return this._evalNative(fname, fw.data.argm(arguments,1));
}

fw.eval._evalNative = function(fname, fparam, isptr){
    if(!isptr && !fw.data.isStr(fname)) return fw.data.error(2);
    if(isptr && !fw.data.isFun(fname)) return fw.data.error(2);
    if(!fw.data.isObj(fparam)) return fw.data.error(2);
    
    var arg = '';
    if(fw.data.isArr(fparam)){
        var sz = fparam.length;
        for(var i=0;i < sz;i++) arg += (arg? ',':'') + 'fparam['+i+']';
    }else{
        for(var i in fparam) arg += (arg? ',':'') + 'fparam['+i+']';
    }
    
    arg = (isptr? ('fname(' + arg + ')') : (fname + '(' + arg + ')'));
    
    try{
        var r = eval(arg);
        return r;
    }catch(e){ return fw.data.error(1); }
}

fw.eval.evalFlat = function(fname){
    var edata = null;
    var evl = fw.data.funFlat(fname, fw.data.argm(arguments,1));
    try{
        edata = this.eval(evl);
        return edata;
    }catch(e){ return fw.data.error(1); }
}

fw.eval.eval = function(data,target){
    var edata = null;
    try{
	if(typeof data != 'string'){
            eval('edata = data');
	}else{
            eval((target? 'edata=' : '') + data);
        }
        return edata;
    }catch(e){ return fw.data.error(1); }
}

fw.eval();