[script] Generate error messages on erroneous operations

This commit is contained in:
Charlie Brej 2009-08-31 01:17:56 +01:00
parent e84fc98325
commit 4539abfba2
2 changed files with 33 additions and 7 deletions

View file

@ -30,10 +30,12 @@ static ply_hashtable_t *script_debug_name_hash = NULL;
static void script_debug_setup (void)
{
if (script_debug_location_hash) return;
script_debug_location_hash = ply_hashtable_new(NULL, NULL);
script_debug_name_hash = ply_hashtable_new(ply_hashtable_string_hash,
ply_hashtable_string_compare);
if (!script_debug_location_hash)
{
script_debug_location_hash = ply_hashtable_new(NULL, NULL);
script_debug_name_hash = ply_hashtable_new(ply_hashtable_string_hash,
ply_hashtable_string_compare);
}
}
void script_debug_add_element (void *element,
@ -63,7 +65,7 @@ void script_debug_remove_element (void *element)
script_debug_location_t *script_debug_lookup_element (void *element)
{
script_debug_setup();
script_debug_location_t *location = ply_hashtable_remove (script_debug_location_hash,
script_debug_location_t *location = ply_hashtable_lookup (script_debug_location_hash,
element);
return NULL;
return location;
}

View file

@ -22,6 +22,7 @@
#define _GNU_SOURCE
#include "ply-hashtable.h"
#include "ply-list.h"
#include "ply-logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
@ -33,6 +34,7 @@
#include <math.h>
#include "script.h"
#include "script-debug.h"
#include "script-execute.h"
#include "script-object.h"
@ -42,6 +44,22 @@ static script_return_t script_execute_function_with_parlist (script_state_t *
script_function_t *function,
ply_list_t *parameter_data);
static void script_execute_error (void *element,
const char *message)
{
script_debug_location_t *location = script_debug_lookup_element (element);
if (location)
ply_error ("Execution error \"%s\" L:%d C:%d : %s\n",
location->name,
location->line_index,
location->column_index,
message);
else
ply_error ("Execution error: %s\n", message);
}
static script_obj_t *script_evaluate_apply_function (script_state_t *state,
script_exp_t *exp,
script_obj_t *(*function)(script_obj_t *, script_obj_t *))
@ -183,7 +201,11 @@ static script_obj_t *script_evaluate_unary (script_state_t *state,
{
if (script_obj_is_number(obj))
new_obj = script_obj_new_number (-script_obj_as_number (obj));
else new_obj = script_obj_new_null ();
else
{
script_execute_error(exp, "Cannot negate non number objects");
new_obj = script_obj_new_null ();
}
script_obj_unref (obj);
return new_obj;
}
@ -206,6 +228,7 @@ static script_obj_t *script_evaluate_unary (script_state_t *state,
}
else
{
script_execute_error(exp, "Cannot increment/decrement non number objects");
new_obj = script_obj_new_null (); /* If performeing something like a=hash++; a and hash become NULL */
script_obj_reset (obj);
}
@ -222,6 +245,7 @@ static script_obj_t *script_evaluate_func (script_state_t *state,
if (!function)
{
script_execute_error(exp, "Call operated on an object with is not a function");
script_obj_unref (func_obj);
return script_obj_new_null ();
}