#include #include #include #include "mlp_priv.h" mlp_object *mlp_object_alloc(mlp_runtime* rt, const char *class, void *handle){ mlp_object *o = MLP_MALLOC(mlp_object) ; o->runtime = rt ; o->class = strdup(class) ; o->handle = handle ; o->stringy = NULL ; return o ; } const char *mlp_object_get_class(const mlp_object *obj){ return obj->class ; } void *mlp_object_get_handle(const mlp_object *obj){ return obj->handle ; } mlp_runtime *mlp_object_get_runtime(const mlp_object *obj){ return obj->runtime ; } const char *mlp_object_to_string(mlp_object *obj){ if (obj->stringy != NULL){ return obj->stringy ; } const char *lang = mlp_language_get_name(mlp_runtime_get_language(mlp_object_get_runtime(obj))) ; int len = strlen(obj->class) + strlen(lang) + 64 ; obj->stringy = (char *)malloc(len * sizeof(char)) ; if (obj->handle != NULL){ snprintf(obj->stringy, len-1, "%s, %s, %p", lang, obj->class, obj->handle) ; } else { snprintf(obj->stringy, len-1, "%s, %s, NULL", lang, obj->class) ; } return obj->stringy ; } mlp_value *mlp_object_new(mlp_context *ctx, mlp_runtime* rt, const char *class, mlp_value **args, int nb_args, const char *hint){ mlp_context_clear_error(ctx) ; const mlp_interface *iface = mlp_language_get_iface(rt->language) ; mlp_value *ret = iface->object_new(ctx, rt, class, args, nb_args, hint) ; if (mlp_context_has_error(ctx)){ return NULL ; } return ret ; } mlp_value *mlp_object_call_method(mlp_context *ctx, mlp_object *obj, const char *method, mlp_value **args, int nb_args, const char *hint){ mlp_debug(2, "-> Object call method: %s, %s", mlp_object_to_string(obj), method) ; int i ; for (i = 0 ; i < nb_args ; i++){ mlp_debug(2, "-> %s", mlp_value_to_string(args[i])) ; } mlp_context_clear_error(ctx) ; const mlp_interface *iface = mlp_language_get_iface(obj->runtime->language) ; mlp_value *ret = iface->object_call_method(ctx, obj, method, args, nb_args, hint) ; if (mlp_context_has_error(ctx)){ mlp_debug(1, "!!!!") ; return NULL ; } mlp_debug(2, "<- Object call method") ; return ret ; } void mlp_object_delete(mlp_context *ctx, mlp_object *obj){ mlp_debug(2, "-> Object delete: %s", mlp_object_to_string(obj)) ; mlp_context_clear_error(ctx) ; const mlp_interface *iface = mlp_language_get_iface(obj->runtime->language) ; iface->object_delete(ctx, obj) ; free(obj->class) ; if (obj->stringy != NULL){ free(obj->stringy) ; } free(obj) ; mlp_debug(2, "<- Object delete") ; }