Skip to content
This repository
tree: 2b5b161f8b
Fetching contributors…

Cannot retrieve contributors at this time

file 597 lines (518 sloc) 14.83 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
/*
** object.c - Object, NilClass, TrueClass, FalseClass class
**
** See Copyright Notice in mruby.h
*/

#include "mruby.h"
#include "mruby/class.h"
#include "mruby/numeric.h"
#include "mruby/string.h"
#include "error.h"

int
mrb_obj_eq(mrb_state *mrb, mrb_value v1, mrb_value v2)
{
  if (mrb_type(v1) != mrb_type(v2)) return FALSE;
  switch (mrb_type(v1)) {
  case MRB_TT_TRUE:
    return 1;

  case MRB_TT_FALSE:
  case MRB_TT_FIXNUM:
    return (v1.value.i == v2.value.i);
  case MRB_TT_SYMBOL:
    return (v1.value.sym == v2.value.sym);

  case MRB_TT_FLOAT:
    return (mrb_float(v1) == mrb_float(v2));

  default:
    return (v1.value.p == v2.value.p);
  }
}

int
mrb_obj_equal(mrb_state *mrb, mrb_value v1, mrb_value v2)
{
  /* temporary definition */
  return mrb_obj_eq(mrb, v1, v2);
}

int
mrb_equal(mrb_state *mrb, mrb_value obj1, mrb_value obj2)
{
  mrb_value result;

  if (mrb_obj_eq(mrb, obj1, obj2)) return TRUE;
  result = mrb_funcall(mrb, obj1, "==", 1, obj2);
  if (mrb_test(result)) return TRUE;
  return FALSE;
}

/*
* Document-class: NilClass
*
* The class of the singleton object <code>nil</code>.
*/

/* 15.2.4.3.4 */
/*
* call_seq:
* nil.nil? -> true
*
* Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
*/

static mrb_value
mrb_true(mrb_state *mrb, mrb_value obj)
{
    return mrb_true_value();
}

/* 15.2.4.3.5 */
/*
* call-seq:
* nil.to_s -> ""
*
* Always returns the empty string.
*/

static mrb_value
nil_to_s(mrb_state *mrb, mrb_value obj)
{
  return mrb_str_new(mrb, 0, 0);
}

static mrb_value
nil_inspect(mrb_state *mrb, mrb_value obj)
{
  return mrb_str_new(mrb, "nil", 3);
}

/***********************************************************************
* Document-class: TrueClass
*
* The global value <code>true</code> is the only instance of class
* <code>TrueClass</code> and represents a logically true value in
* boolean expressions. The class provides operators allowing
* <code>true</code> to be used in logical expressions.
*/

/* 15.2.5.3.1 */
/*
* call-seq:
* true & obj -> true or false
*
* And---Returns <code>false</code> if <i>obj</i> is
* <code>nil</code> or <code>false</code>, <code>true</code> otherwise.
*/

static mrb_value
true_and(mrb_state *mrb, mrb_value obj)
{
  int obj2;

  mrb_get_args(mrb, "b", &obj2);

  return mrb_bool_value(obj2);
}

/* 15.2.5.3.2 */
/*
* call-seq:
* true ^ obj -> !obj
*
* Exclusive Or---Returns <code>true</code> if <i>obj</i> is
* <code>nil</code> or <code>false</code>, <code>false</code>
* otherwise.
*/

static mrb_value
true_xor(mrb_state *mrb, mrb_value obj)
{
  int obj2;

  mrb_get_args(mrb, "b", &obj2);
  return mrb_bool_value(!obj2);
}

/* 15.2.5.3.3 */
/*
* call-seq:
* true.to_s -> "true"
*
* The string representation of <code>true</code> is "true".
*/

static mrb_value
true_to_s(mrb_state *mrb, mrb_value obj)
{
  return mrb_str_new(mrb, "true", 4);
}

/* 15.2.5.3.4 */
/*
* call-seq:
* true | obj -> true
*
* Or---Returns <code>true</code>. As <i>anObject</i> is an argument to
* a method call, it is always evaluated; there is no short-circuit
* evaluation in this case.
*
* true | puts("or")
* true || puts("logical or")
*
* <em>produces:</em>
*
* or
*/

static mrb_value
true_or(mrb_state *mrb, mrb_value obj)
{
  int obj2;

  mrb_get_args(mrb, "b", &obj2);
  return mrb_true_value();
}

/*
* Document-class: FalseClass
*
* The global value <code>false</code> is the only instance of class
* <code>FalseClass</code> and represents a logically false value in
* boolean expressions. The class provides operators allowing
* <code>false</code> to participate correctly in logical expressions.
*
*/

/* 15.2.4.3.1 */
/* 15.2.6.3.1 */
/*
* call-seq:
* false & obj -> false
* nil & obj -> false
*
* And---Returns <code>false</code>. <i>obj</i> is always
* evaluated as it is the argument to a method call---there is no
* short-circuit evaluation in this case.
*/

static mrb_value
false_and(mrb_state *mrb, mrb_value obj)
{
  int obj2;

  mrb_get_args(mrb, "b", &obj2);
  return mrb_false_value();
}

/* 15.2.4.3.2 */
/* 15.2.6.3.2 */
/*
* call-seq:
* false ^ obj -> true or false
* nil ^ obj -> true or false
*
* Exclusive Or---If <i>obj</i> is <code>nil</code> or
* <code>false</code>, returns <code>false</code>; otherwise, returns
* <code>true</code>.
*
*/

static mrb_value
false_xor(mrb_state *mrb, mrb_value obj)
{
  int obj2;

  mrb_get_args(mrb, "b", &obj2);
  return mrb_bool_value(obj2);
}

/* 15.2.4.3.3 */
/* 15.2.6.3.4 */
/*
* call-seq:
* false | obj -> true or false
* nil | obj -> true or false
*
* Or---Returns <code>false</code> if <i>obj</i> is
* <code>nil</code> or <code>false</code>; <code>true</code> otherwise.
*/

static mrb_value
false_or(mrb_state *mrb, mrb_value obj)
{
  int obj2;

  mrb_get_args(mrb, "b", &obj2);
  return mrb_bool_value(obj2);
}

/* 15.2.6.3.3 */
/*
* call-seq:
* false.to_s -> "false"
*
* 'nuf said...
*/

static mrb_value
false_to_s(mrb_state *mrb, mrb_value obj)
{
  return mrb_str_new(mrb, "false", 5);
}

void
mrb_init_object(mrb_state *mrb)
{
  struct RClass *n;
  struct RClass *t;
  struct RClass *f;

  n = mrb->nil_class = mrb_define_class(mrb, "NilClass", mrb->object_class);
  mrb_undef_class_method(mrb, n, "new");
  mrb_define_method(mrb, n, "&", false_and, ARGS_REQ(1)); /* 15.2.4.3.1 */
  mrb_define_method(mrb, n, "^", false_xor, ARGS_REQ(1)); /* 15.2.4.3.2 */
  mrb_define_method(mrb, n, "|", false_or, ARGS_REQ(1)); /* 15.2.4.3.3 */
  mrb_define_method(mrb, n, "nil?", mrb_true, ARGS_NONE()); /* 15.2.4.3.4 */
  mrb_define_method(mrb, n, "to_s", nil_to_s, ARGS_NONE()); /* 15.2.4.3.5 */
  mrb_define_method(mrb, n, "inspect", nil_inspect, ARGS_NONE());

  t = mrb->true_class = mrb_define_class(mrb, "TrueClass", mrb->object_class);
  mrb_undef_class_method(mrb, t, "new");
  mrb_define_method(mrb, t, "&", true_and, ARGS_REQ(1)); /* 15.2.5.3.1 */
  mrb_define_method(mrb, t, "^", true_xor, ARGS_REQ(1)); /* 15.2.5.3.2 */
  mrb_define_method(mrb, t, "to_s", true_to_s, ARGS_NONE()); /* 15.2.5.3.3 */
  mrb_define_method(mrb, t, "|", true_or, ARGS_REQ(1)); /* 15.2.5.3.4 */
  mrb_define_method(mrb, t, "inspect", true_to_s, ARGS_NONE());

  f = mrb->false_class = mrb_define_class(mrb, "FalseClass", mrb->object_class);
  mrb_undef_class_method(mrb, f, "new");
  mrb_define_method(mrb, f, "&", false_and, ARGS_REQ(1)); /* 15.2.6.3.1 */
  mrb_define_method(mrb, f, "^", false_xor, ARGS_REQ(1)); /* 15.2.6.3.2 */
  mrb_define_method(mrb, f, "to_s", false_to_s, ARGS_NONE()); /* 15.2.6.3.3 */
  mrb_define_method(mrb, f, "|", false_or, ARGS_REQ(1)); /* 15.2.6.3.4 */
  mrb_define_method(mrb, f, "inspect", false_to_s, ARGS_NONE());
}

static mrb_value
convert_type(mrb_state *mrb, mrb_value val, const char *tname, const char *method, int raise)
{
  mrb_sym m = 0;

  m = mrb_intern(mrb, method);
  if (!mrb_respond_to(mrb, val, m)) {
    if (raise) {
      mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into %S", val, mrb_str_new_cstr(mrb, tname));
      return mrb_nil_value();
    }
    else {
      return mrb_nil_value();
    }
  }
  return mrb_funcall_argv(mrb, val, m, 0, 0);
}

mrb_value
mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method)
{
    mrb_value v;

    if (mrb_type(val) == MRB_TT_FIXNUM) return val;
    v = convert_type(mrb, val, "Integer", method, FALSE);
    if (mrb_nil_p(v) || mrb_type(v) != MRB_TT_FIXNUM) {
      return mrb_nil_value();
    }
    return v;
}

mrb_value
mrb_convert_type(mrb_state *mrb, mrb_value val, mrb_int type, const char *tname, const char *method)
{
  mrb_value v;

  if (mrb_type(val) == type) return val;
  v = convert_type(mrb, val, tname, method, 1/*Qtrue*/);
  if (mrb_type(v) != type) {
    mrb_raisef(mrb, E_TYPE_ERROR, "%S cannot be converted to %S by #%S", val,
               mrb_str_new_cstr(mrb, tname), mrb_str_new_cstr(mrb, method));
  }
  return v;
}

mrb_value
mrb_check_convert_type(mrb_state *mrb, mrb_value val, mrb_int type, const char *tname, const char *method)
{
  mrb_value v;

  if (mrb_type(val) == type && type != MRB_TT_DATA) return val;
  v = convert_type(mrb, val, tname, method, 0/*Qfalse*/);
  if (mrb_nil_p(v) || mrb_type(v) != type) return mrb_nil_value();
  return v;
}

static const struct types {
    unsigned char type;
    const char *name;
} builtin_types[] = {
// {MRB_TT_NIL, "nil"},
  {MRB_TT_FALSE, "false"},
  {MRB_TT_TRUE, "true"},
  {MRB_TT_FIXNUM, "Fixnum"},
  {MRB_TT_SYMBOL, "Symbol"}, /* :symbol */
  {MRB_TT_MODULE, "Module"},
  {MRB_TT_OBJECT, "Object"},
  {MRB_TT_CLASS, "Class"},
  {MRB_TT_ICLASS, "iClass"}, /* internal use: mixed-in module holder */
  {MRB_TT_SCLASS, "SClass"},
  {MRB_TT_PROC, "Proc"},
  {MRB_TT_FLOAT, "Float"},
  {MRB_TT_ARRAY, "Array"},
  {MRB_TT_HASH, "Hash"},
  {MRB_TT_STRING, "String"},
  {MRB_TT_RANGE, "Range"},
// {MRB_TT_BIGNUM, "Bignum"},
  {MRB_TT_FILE, "File"},
  {MRB_TT_DATA, "Data"}, /* internal use: wrapped C pointers */
// {MRB_TT_VARMAP, "Varmap"}, /* internal use: dynamic variables */
// {MRB_TT_NODE, "Node"}, /* internal use: syntax tree node */
// {MRB_TT_UNDEF, "undef"}, /* internal use: #undef; should not happen */
    {-1, 0}
};

void
mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t)
{
  const struct types *type = builtin_types;
  struct RString *s;
  enum mrb_vtype xt;

  xt = mrb_type(x);
  if ((xt != t) || (xt == MRB_TT_DATA)) {
    while (type->type < MRB_TT_MAXDEFINE) {
      if (type->type == t) {
        const char *etype;

        if (mrb_nil_p(x)) {
          etype = "nil";
        }
        else if (mrb_type(x) == MRB_TT_FIXNUM) {
          etype = "Fixnum";
        }
        else if (mrb_type(x) == MRB_TT_SYMBOL) {
          etype = "Symbol";
        }
        else if (mrb_special_const_p(x)) {
          s = mrb_str_ptr(mrb_obj_as_string(mrb, x));
          etype = s->ptr;
        }
        else {
          etype = mrb_obj_classname(mrb, x);
        }
        mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %S (expected %S)",
                   mrb_str_new_cstr(mrb, etype), mrb_str_new_cstr(mrb, type->name));
      }
      type++;
    }
    mrb_raisef(mrb, E_TYPE_ERROR, "unknown type %S (%S given)",
               mrb_fixnum_value(t), mrb_fixnum_value(mrb_type(x)));
  }
}

/* 15.3.1.3.46 */
/*
* call-seq:
* obj.to_s => string
*
* Returns a string representing <i>obj</i>. The default
* <code>to_s</code> prints the object's class and an encoding of the
* object id. As a special case, the top-level object that is the
* initial execution context of Ruby programs returns ``main.''
*/

mrb_value
mrb_any_to_s(mrb_state *mrb, mrb_value obj)
{
  mrb_value str = mrb_str_buf_new(mrb, 20);
  const char *cname = mrb_obj_classname(mrb, obj);

  mrb_str_buf_cat(mrb, str, "#<", 2);
  mrb_str_cat2(mrb, str, cname);
  mrb_str_concat(mrb, str, mrb_ptr_to_str(mrb, mrb_voidp(obj)));
  mrb_str_buf_cat(mrb, str, ">", 1);

  return str;
}

/*
* call-seq:
* obj.is_a?(class) => true or false
* obj.kind_of?(class) => true or false
*
* Returns <code>true</code> if <i>class</i> is the class of
* <i>obj</i>, or if <i>class</i> is one of the superclasses of
* <i>obj</i> or modules included in <i>obj</i>.
*
* module M; end
* class A
* include M
* end
* class B < A; end
* class C < B; end
* b = B.new
* b.instance_of? A #=> false
* b.instance_of? B #=> true
* b.instance_of? C #=> false
* b.instance_of? M #=> false
* b.kind_of? A #=> true
* b.kind_of? B #=> true
* b.kind_of? C #=> false
* b.kind_of? M #=> true
*/

int
mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c)
{
  struct RClass *cl = mrb_class(mrb, obj);

  switch (c->tt) {
    case MRB_TT_MODULE:
    case MRB_TT_CLASS:
    case MRB_TT_ICLASS:
      break;

    default:
      mrb_raise(mrb, E_TYPE_ERROR, "class or module required");
  }

  while (cl) {
    if (cl == c || cl->mt == c->mt)
      return TRUE;
    cl = cl->super;
  }
  return FALSE;
}

static mrb_value
mrb_to_integer(mrb_state *mrb, mrb_value val, const char *method)
{
    mrb_value v;

    if (mrb_fixnum_p(val)) return val;
    v = convert_type(mrb, val, "Integer", method, TRUE);
    if (!mrb_obj_is_kind_of(mrb, v, mrb->fixnum_class)) {
      mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S to Integer (%S#%S gives %S)",
                 val, val, mrb_str_new_cstr(mrb, method), v);
    }
    return v;
}

mrb_value
mrb_to_int(mrb_state *mrb, mrb_value val)
{
    return mrb_to_integer(mrb, val, "to_int");
}

static mrb_value
mrb_convert_to_integer(mrb_state *mrb, mrb_value val, int base)
{
  mrb_value tmp;

  if (mrb_nil_p(val)) {
    if (base != 0) goto arg_error;
      mrb_raise(mrb, E_TYPE_ERROR, "can't convert nil into Integer");
  }
  switch (mrb_type(val)) {
    case MRB_TT_FLOAT:
      if (base != 0) goto arg_error;
      if (FIXABLE(mrb_float(val))) {
          break;
      }
      return mrb_flt2big(mrb, mrb_float(val));

    case MRB_TT_FIXNUM:
      if (base != 0) goto arg_error;
      return val;

    case MRB_TT_STRING:
string_conv:
      return mrb_str_to_inum(mrb, val, base, TRUE);

    default:
      break;
  }
  if (base != 0) {
      tmp = mrb_check_string_type(mrb, val);
      if (!mrb_nil_p(tmp)) goto string_conv;
    arg_error:
      mrb_raise(mrb, E_ARGUMENT_ERROR, "base specified for non string value");
  }
  tmp = convert_type(mrb, val, "Integer", "to_int", FALSE);
  if (mrb_nil_p(tmp)) {
      return mrb_to_integer(mrb, val, "to_i");
  }
  return tmp;
}

mrb_value
mrb_Integer(mrb_state *mrb, mrb_value val)
{
    return mrb_convert_to_integer(mrb, val, 0);
}

mrb_value
mrb_Float(mrb_state *mrb, mrb_value val)
{
  if (mrb_nil_p(val)) {
      mrb_raise(mrb, E_TYPE_ERROR, "can't convert nil into Float");
  }
  switch (mrb_type(val)) {
    case MRB_TT_FIXNUM:
      return mrb_float_value((mrb_float)mrb_fixnum(val));

    case MRB_TT_FLOAT:
      return val;

    case MRB_TT_STRING:
      return mrb_float_value(mrb_str_to_dbl(mrb, val, TRUE));

    default:
      return mrb_convert_type(mrb, val, MRB_TT_FLOAT, "Float", "to_f");
  }
}

mrb_value
mrb_inspect(mrb_state *mrb, mrb_value obj)
{
  return mrb_obj_as_string(mrb, mrb_funcall(mrb, obj, "inspect", 0, 0));
}

int
mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2)
{
  if (mrb_obj_eq(mrb, obj1, obj2)) return TRUE;
  return mrb_test(mrb_funcall(mrb, obj1, "eql?", 1, obj2));
}
Something went wrong with that request. Please try again.