Comunidad de diseño web y desarrollo en internet online

un string que es una linea de codigo como la ejecuto

Citar            
MensajeEscrito el 05 Abr 2010 04:27 am
hola que tal, quería saber si alguien conocía una librería que interpretara una linea de codigo que tengo como string y la ejecutara, por ejemplo:

var string1 = "mi_funcion(objeto.hola.hola2,objeto.hola.hola2)"
var string2 = "function(){return Math.random(5)}"
var string3 = "evil = 665 + 1"

son strings que con ayuda de alguna libreria podrían ser ejecutados como lineas de codigo

los necesito para un sistema de interacción dinámico de cliente servidor para un juego.

alguien sabe?

Por fermmm

Claber

122 de clabLevel



 

chrome
Citar            
MensajeEscrito el 05 Abr 2010 10:50 am

Por Eliseo2

710 de clabLevel



 

firefox
Citar            
MensajeEscrito el 05 Abr 2010 11:13 am
muchas gracias

Por fermmm

Claber

122 de clabLevel



 

firefox
Citar            
MensajeEscrito el 08 Abr 2010 09:38 am
Antes de que me respondieras programe mi propio sistema para ejecutar lines de codigo que son strings, se puede considerar una version limitada mas amigable para los novatos... tal vez... Así que la posteo por si alguien la necesita:

Conversion de string a linea de codigo:

Código ActionScript :

/////////////////////////////////////////////////

// La funcion "ejecutar_linea_desde_string(string)" ejecuta una linea de codigo escrita como string, es util para enviar una orden especifica al servidor sin tener que programar
// nada especifico en el servidor para esa orden, muy bueno para debugear cliente servidor.
// Soporta ejecucion de funciones locales como tambien que estan dentro de un objeto que esta dentro de otro objeto, lo mismo esta soportado con las variables
// y con los parametros de las funciones. Son soportados un maximo de 10 parametros en una funcion y maximo 10 niveles de una variable.
// Soporta definicion de variables (no creacion), tambien soporta definicion de variables que son una funcion o que implican ejecutar una funcion, por ejemplo "variable = funcion(parametro)".
// Ninguna otra cosa que no sea lo nombrado recien esta soportado por estas funciones, tampoco hay que programar con simbolos como ; , :, [] o {} , no se puede hacer lo siguiente:
// "array = ["hola", "que tal"]" o Objeto = {hola:123} ni usar un sinbolo de finalizar linea, por ejemplo: "variable = variable2;" . Para tener mas libertad se puede usar una libreria que permite 
// programar cualquier cosa de cualquier manera desde un string, se encuentra en este link:  http://eval.hurlant.com/


trace(ejecutar_linea_desde_string("variable = funcion(variable2.variable3"))


/////////////////////////////////////////////////////


////////// Las funciones


function ejecutar_linea_desde_string(string){
   string = string.split(" = ").join("=");
   string = string.split(" =").join("=");
   string = string.split("= ").join("=");
   string = string.split(") ").join(")");
   string = string.split(" (").join("(");
   string = string.split("( ").join("(");

   var lados_igualdad = string.split("=")
   if(lados_igualdad.length == 2){
      buscar_ruta_desde_string(lados_igualdad[0],ejecutar_funcion_desde_string(lados_igualdad[1]))
   }
   if(lados_igualdad.length == 1){
      return ejecutar_funcion_desde_string(lados_igualdad[0])
   }
}


// Esta funcion ejecuta una funcion y retorna lo que retorne esa funcion si no es una funcion retorna el objeto que sea
function ejecutar_funcion_desde_string(string){
   var arraytemp2 = string.split("(")
   if(arraytemp2.length > 1){
      if(arraytemp2[1] != ")"){
         var funcion_temp = buscar_ruta_desde_string(arraytemp2[0])
         var dentro_de_parentesis = arraytemp2[1].split(")")
         var parametros = dentro_de_parentesis[0].split(",")
         if (parametros.length > 1){
            if(parametros.length == 2){
               return funcion_temp(ejecutar_funcion_desde_string(parametros[0]),ejecutar_funcion_desde_string(parametros[1]))
            }else if(parametros.length == 3){
               return funcion_temp(ejecutar_funcion_desde_string(parametros[0]),ejecutar_funcion_desde_string(parametros[1]),ejecutar_funcion_desde_string(parametros[2]))
            }else if(parametros.length == 4){
               return funcion_temp(ejecutar_funcion_desde_string(parametros[0]),ejecutar_funcion_desde_string(parametros[1]),ejecutar_funcion_desde_string(parametros[2]),ejecutar_funcion_desde_string(parametros[3]))
            }else if(parametros.length == 5){
               return funcion_temp(ejecutar_funcion_desde_string(parametros[0]),ejecutar_funcion_desde_string(parametros[1]),ejecutar_funcion_desde_string(parametros[2]),ejecutar_funcion_desde_string(parametros[3]),ejecutar_funcion_desde_string(parametros[4]))
            }else if(parametros.length == 6){
               return funcion_temp(ejecutar_funcion_desde_string(parametros[0]),ejecutar_funcion_desde_string(parametros[1]),ejecutar_funcion_desde_string(parametros[2]),ejecutar_funcion_desde_string(parametros[3]),ejecutar_funcion_desde_string(parametros[4]),ejecutar_funcion_desde_string(parametros[5]))
            }else if(parametros.length == 7){
               return funcion_temp(ejecutar_funcion_desde_string(parametros[0]),ejecutar_funcion_desde_string(parametros[1]),ejecutar_funcion_desde_string(parametros[2]),ejecutar_funcion_desde_string(parametros[3]),ejecutar_funcion_desde_string(parametros[4]),ejecutar_funcion_desde_string(parametros[5]),ejecutar_funcion_desde_string(parametros[6]))
            }else if(parametros.length == 8){
               return funcion_temp(ejecutar_funcion_desde_string(parametros[0]),ejecutar_funcion_desde_string(parametros[1]),ejecutar_funcion_desde_string(parametros[2]),ejecutar_funcion_desde_string(parametros[3]),ejecutar_funcion_desde_string(parametros[4]),ejecutar_funcion_desde_string(parametros[5]),ejecutar_funcion_desde_string(parametros[6]),ejecutar_funcion_desde_string(parametros[7]))
            }else if(parametros.length == 9){
               return funcion_temp(ejecutar_funcion_desde_string(parametros[0]),ejecutar_funcion_desde_string(parametros[1]),ejecutar_funcion_desde_string(parametros[2]),ejecutar_funcion_desde_string(parametros[3]),ejecutar_funcion_desde_string(parametros[4]),ejecutar_funcion_desde_string(parametros[5]),ejecutar_funcion_desde_string(parametros[6]),ejecutar_funcion_desde_string(parametros[7]),ejecutar_funcion_desde_string(parametros[8]))
            }else if(parametros.length == 10){
               return funcion_temp(ejecutar_funcion_desde_string(parametros[0]),ejecutar_funcion_desde_string(parametros[1]),ejecutar_funcion_desde_string(parametros[2]),ejecutar_funcion_desde_string(parametros[3]),ejecutar_funcion_desde_string(parametros[4]),ejecutar_funcion_desde_string(parametros[5]),ejecutar_funcion_desde_string(parametros[6]),ejecutar_funcion_desde_string(parametros[7]),ejecutar_funcion_desde_string(parametros[8]),ejecutar_funcion_desde_string(parametros[9]))
            }else if(parametros.length > 10){
               trace("Error se reciben demasiados parametros en ejecucion de funcion remota")
            }
         }else{
            return funcion_temp(buscar_ruta_desde_string(parametros[0]))
         }
      }else{
         //es una funcion sin parametros
         return this[arraytemp2[0]]()
      }
   }else{
      // no es una funcion
      return buscar_ruta_desde_string(string)
   }
}
/// buscar ruta


// Esta funcion detecta si un string es una variable o si es la variable de un objeto
// y devuelve dicha variable, si se le asigna algo que no es un string lo devuelve sin tocarlo.
function buscar_ruta_desde_string(parametro,elemento_resultante_es_igual_a = null){
   var ruta
   if(typeof(parametro) == "string"){
      var arraytemp = parametro.split(".")
      if(arraytemp.length > 1){
         for (var i:int = 0; i < arraytemp.length; i++){
            if(i == 0){
               ruta = this[arraytemp[i]]
            }else{
               ruta = ruta[arraytemp[i]]
            }
         }
         if(elemento_resultante_es_igual_a != null){
            if(arraytemp.length == 2){
               this[arraytemp[0]][arraytemp[1]] = elemento_resultante_es_igual_a
            }else if(arraytemp.length == 3){
               this[arraytemp[0]][arraytemp[1]][arraytemp[2]] = elemento_resultante_es_igual_a
            }else if(arraytemp.length == 4){
               this[arraytemp[0]][arraytemp[1]][arraytemp[2]][arraytemp[3]] = elemento_resultante_es_igual_a
            }else if(arraytemp.length == 5){
               this[arraytemp[0]][arraytemp[1]][arraytemp[2]][arraytemp[3]][arraytemp[4]] = elemento_resultante_es_igual_a
            }else if(arraytemp.length == 6){
               this[arraytemp[0]][arraytemp[1]][arraytemp[2]][arraytemp[3]][arraytemp[4]][arraytemp[5]] = elemento_resultante_es_igual_a
            }else if(arraytemp.length == 7){
               this[arraytemp[0]][arraytemp[1]][arraytemp[2]][arraytemp[3]][arraytemp[4]][arraytemp[5]][arraytemp[6]] = elemento_resultante_es_igual_a
            }else if(arraytemp.length == 8){
               this[arraytemp[0]][arraytemp[1]][arraytemp[2]][arraytemp[3]][arraytemp[4]][arraytemp[5]][arraytemp[6]][arraytemp[7]] = elemento_resultante_es_igual_a
            }else if(arraytemp.length == 9){
               this[arraytemp[0]][arraytemp[1]][arraytemp[2]][arraytemp[3]][arraytemp[4]][arraytemp[5]][arraytemp[6]][arraytemp[7]][arraytemp[8]] = elemento_resultante_es_igual_a
            }else if(arraytemp.length == 10){
               this[arraytemp[0]][arraytemp[1]][arraytemp[2]][arraytemp[3]][arraytemp[4]][arraytemp[5]][arraytemp[6]][arraytemp[7]][arraytemp[8]][arraytemp[9]] = elemento_resultante_es_igual_a
            }else if(arraytemp.length > 10){
               trace("error me estoy metiendo demasiado adentro de un objeto no lo soporto mas")
            }
         }
      } else {
         // Si no hay puntos en el string, tal vez el resultado tiene que ser interpretado como string y no como una variable asi que comprobamos si existe como variable
         if(this[arraytemp[0]] != undefined && this[arraytemp[0]] != null){
            ruta = this[arraytemp[0]]
            
            if(elemento_resultante_es_igual_a != null){
               this[arraytemp[0]] = elemento_resultante_es_igual_a
            }
         }else{
            // Si no existe la devolvemos como string
            ruta = arraytemp[0]
         }
      }
      return ruta
   }else{
      // Si no es un string devuelve el parametro sin tocarlo
      return parametro
   }
}

Por fermmm

Claber

122 de clabLevel



 

chrome

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.