Branch: Tag:

1998-02-20

1998-02-20 00:51:16 by Fredrik Hübinette (Hubbe) <hubbe@hubbe.net>

cast changed

Rev: src/docode.c:1.31
Rev: src/modules/Gmp/mpz_glue.c:1.30
Rev: src/opcodes.c:1.12

18:   #include "pike_types.h"   #include "pike_memory.h"   #include "fd_control.h" + #include "cyclic.h" + #include "builtin_functions.h"      void index_no_free(struct svalue *to,struct svalue *what,struct svalue *ind)   {
213:    }   }    - void f_cast(void) + void o_cast(struct pike_string *type, INT32 run_time_type)   {    INT32 i;    -  i=compile_type_to_runtime_type(sp[-1].u.string); -  -  if(i != sp[-2].type) +  if(run_time_type != sp[-1].type)    { -  if(i == T_MIXED) -  { -  pop_stack(); +  if(run_time_type == T_MIXED)    return; -  } +     -  if(sp[-2].type == T_OBJECT) +  if(sp[-1].type == T_OBJECT)    {    struct pike_string *s; -  s=describe_type(sp[-1].u.string); -  pop_stack(); +  s=describe_type(type);    push_string(s);    if(!sp[-2].u.object->prog)    error("Cast called on destructed object.\n");
244:    return;    }    -  pop_stack(); -  switch(i) +  switch(run_time_type)    {    case T_MIXED:    break;    -  +  case T_ARRAY: +  switch(sp[-1].type) +  { +  case T_MAPPING: +  { +  struct array *a=mapping_to_array(sp[-1].u.mapping); +  pop_stack(); +  push_array(a); +  break; +  } +  +  case T_STRING: +  f_values(1); +  break; +  +  case T_MULTISET: +  f_indices(1); +  break; +  +  default: +  error("Cannot cast to array.\n"); +  +  } +  break; +     case T_INT:    switch(sp[-1].type)    {
328:    push_int(0);    }    APPLY_MASTER("cast_to_object",2); -  break; +  return;       case T_FUNCTION:    sp[-1].type = T_OBJECT;
345:    push_int(0);    }    APPLY_MASTER("cast_to_program",2); -  break; +  return;       case T_FUNCTION:    {
370:    }       } +  } +  + #ifdef DEBUG +  if(run_time_type != sp[-1].type) +  fatal("Internal error: Cast failed.\n"); + #endif +  +  switch(run_time_type) +  { +  case T_ARRAY: +  { +  struct pike_string *itype; +  INT32 run_time_itype; +  +  push_string(itype=index_type(sp[-2].u.string,0)); +  run_time_itype=compile_type_to_runtime_type(itype); +  +  if(run_time_itype != T_MIXED) +  { +  struct array *a; +  struct array *tmp=sp[-2].u.array; +  DECLARE_CYCLIC(); +  +  if((a=(struct array *)BEGIN_CYCLIC(tmp,0))) +  { +  ref_push_array(a);    }else{ -  +  INT32 e,i; +  struct pike_string *s; +  push_array(a=allocate_array(tmp->size)); +  SET_CYCLIC_RET(a); +  +  for(e=0;e<a->size;e++) +  { +  push_svalue(tmp->item+e); +  o_cast(itype, run_time_itype); +  array_set_index(a,e,sp-1);    pop_stack();    } -  +  END_CYCLIC();    } -  +  assign_svalue(sp-3,sp-1); +  pop_stack(); +  } +  pop_stack(); +  } +  } + }       -  + void f_cast(void) + { + #ifdef DEBUG +  struct svalue *save_sp=sp; + #endif +  o_cast(sp[-2].u.string, +  compile_type_to_runtime_type(sp[-2].u.string)); + #ifdef DEBUG +  if(save_sp != sp) +  fatal("Internal error: o_cast() left droppings on stack.\n"); + #endif +  free_svalue(sp-2); +  sp[-2]=sp[-1]; +  sp--; + } +  +    /*    flags:    *