Branch: Tag:

2014-12-04

2014-12-04 19:22:56 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Operators: Casting of objects to programs supported for all objects.

The cast operator now falls back to object_program() when casting objects
to programs when there is no lfun::cast(), or it returned UNDEFINED.

The old behaviour for the above cases was to throw either of the errors:
"No cast method in object.\n"
or
"Cast failed, wanted program, got int\n"

Needed to implement [bug 6573 (#6573)].

508:    {    struct object *o = sp[-1].u.object;    int f = FIND_LFUN(o->prog->inherits[SUBTYPEOF(sp[-1])].prog, LFUN_CAST); -  if(f == -1) +  if(f == -1) { +  if (run_time_type != T_PROGRAM) {    Pike_error("No cast method in object.\n"); -  +  } +  f_object_program(1); +  return; +  }    push_string(describe_type(type));    apply_low(o, f, 1); -  +  +  if (run_time_type == T_PROGRAM) { +  if (IS_UNDEFINED(Pike_sp-1)) { +  pop_stack(); +  f_object_program(1); +  return; +  } +  } +     stack_pop_keep_top(); -  }else +     -  +  } else +     switch(run_time_type)    {    default:
713:       if(run_time_type != TYPEOF(sp[-1]))    { -  if(TYPEOF(sp[-1]) == T_OBJECT && sp[-1].u.object->prog) +  switch(TYPEOF(sp[-1])) { +  case T_OBJECT: +  if(sp[-1].u.object->prog)    {    struct object *o = sp[-1].u.object;    int f = FIND_LFUN(o->prog->inherits[SUBTYPEOF(sp[-1])].prog,
727:    if(f) goto emulated_type_ok;    }    } +  break; +  case T_FUNCTION: +  /* Check that the function actually is a program. */ +  if ((run_time_type == T_PROGRAM) && +  program_from_function(sp-1)) { +  return; /* No need for further post-processing. */ +  } +  break; +  }    Pike_error("Cast failed, wanted %s, got %s\n",    get_name_of_type(run_time_type),    get_name_of_type(TYPEOF(sp[-1])));