Ejemplo n.º 1
0
Archivo: hash.c Proyecto: denji/mruby
static mrb_value
inspect_hash(mrb_state *mrb, mrb_value hash, int recur)
{
  mrb_value str, str2;
  khash_t(ht) *h = RHASH_TBL(hash);
  khiter_t k;

  if (recur) return mrb_str_new_lit(mrb, "{...}");

  str = mrb_str_new_lit(mrb, "{");
  if (h && kh_size(h) > 0) {
    for (k = kh_begin(h); k != kh_end(h); k++) {
      int ai;

      if (!kh_exist(h,k)) continue;

      ai = mrb_gc_arena_save(mrb);

      if (RSTRING_LEN(str) > 1) mrb_str_cat_lit(mrb, str, ", ");

      str2 = mrb_inspect(mrb, kh_key(h,k));
      mrb_str_append(mrb, str, str2);
      mrb_str_cat_lit(mrb, str, "=>");
      str2 = mrb_inspect(mrb, kh_value(h,k));
      mrb_str_append(mrb, str, str2);

      mrb_gc_arena_restore(mrb, ai);
    }
  }
  mrb_str_cat_lit(mrb, str, "}");

  return str;
}
Ejemplo n.º 2
0
static mrb_value
method_to_s(mrb_state *mrb, mrb_value self)
{
  mrb_value owner = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@owner"));
  mrb_value klass = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@klass"));
  mrb_value name = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@name"));
  mrb_value str = mrb_str_new_lit(mrb, "#<");
  struct RClass *rklass;

  mrb_str_cat_cstr(mrb, str, mrb_obj_classname(mrb, self));
  mrb_str_cat_lit(mrb, str, ": ");
  rklass = mrb_class_ptr(klass);
  if (mrb_class_ptr(owner) == rklass) {
    mrb_str_cat_str(mrb, str, mrb_funcall(mrb, owner, "to_s", 0));
    mrb_str_cat_lit(mrb, str, "#");
    mrb_str_cat_str(mrb, str, mrb_funcall(mrb, name, "to_s", 0));
  }
  else {
    mrb_str_cat_cstr(mrb, str, mrb_class_name(mrb, rklass));
    mrb_str_cat_lit(mrb, str, "(");
    mrb_str_cat_str(mrb, str, mrb_funcall(mrb, owner, "to_s", 0));
    mrb_str_cat_lit(mrb, str, ")#");
    mrb_str_cat_str(mrb, str, mrb_funcall(mrb, name, "to_s", 0));
  }
  mrb_str_cat_lit(mrb, str, ">");
  return str;
}
Ejemplo n.º 3
0
static mrb_value
mrb_proc_inspect(mrb_state *mrb, mrb_value self)
{
  struct RProc *p = mrb_proc_ptr(self);
  mrb_value str = mrb_str_new_lit(mrb, "#<Proc:");
  mrb_str_concat(mrb, str, mrb_ptr_to_str(mrb, mrb_cptr(self)));

  if (!MRB_PROC_CFUNC_P(p)) {
    mrb_irep *irep = p->body.irep;
    const char *filename;
    int32_t line;
    mrb_str_cat_lit(mrb, str, "@");

    filename = mrb_debug_get_filename(irep, 0);
    mrb_str_cat_cstr(mrb, str, filename ? filename : "-");
    mrb_str_cat_lit(mrb, str, ":");

    line = mrb_debug_get_line(irep, 0);
    if (line != -1) {
      mrb_str_append(mrb, str, mrb_fixnum_value(line));
    }
    else {
      mrb_str_cat_lit(mrb, str, "-");
    }
  }

  if (MRB_PROC_STRICT_P(p)) {
    mrb_str_cat_lit(mrb, str, " (lambda)");
  }

  mrb_str_cat_lit(mrb, str, ">");
  return str;
}
Ejemplo n.º 4
0
Archivo: gc.c Proyecto: anehing/mruby
void
test_gc_gray_mark(void)
{
  mrb_state *mrb = mrb_open();
  mrb_value obj_v, value_v;
  struct RBasic *obj;
  size_t gray_num = 0;

  puts("test_gc_gray_mark");

  puts("  in MRB_TT_CLASS");
  obj = (struct RBasic*)mrb->object_class;
  paint_gray(obj);
  gray_num = gc_gray_mark(mrb, obj);
  mrb_assert(is_black(obj));
  mrb_assert(gray_num > 1);

  puts("  in MRB_TT_ARRAY");
  obj_v = mrb_ary_new(mrb);
  value_v = mrb_str_new_lit(mrb, "test");
  paint_gray(mrb_basic_ptr(obj_v));
  paint_partial_white(mrb, mrb_basic_ptr(value_v));
  mrb_ary_push(mrb, obj_v, value_v);
  gray_num = gc_gray_mark(mrb, mrb_basic_ptr(obj_v));
  mrb_assert(is_black(mrb_basic_ptr(obj_v)));
  mrb_assert(is_gray(mrb_basic_ptr(value_v)));
  mrb_assert(gray_num == 1);

  mrb_close(mrb);
}
Ejemplo n.º 5
0
static unsigned int mrb_http2_config_get_worker(mrb_state *mrb, mrb_value args, mrb_value w)
{
  int worker;

  // worker => fixnum or "auto"
  if (!mrb_nil_p(w)) {
    if (mrb_type(w) == MRB_TT_STRING
        && mrb_equal(mrb, w, mrb_str_new_lit(mrb, "auto"))) {
      worker = sysconf(_SC_NPROCESSORS_ONLN);
      if (worker < 0 || worker > MRB_HTTP2_WORKER_MAX) {
        mrb_raise(mrb, E_RUNTIME_ERROR, "failed sysconf(_SC_NPROCESSORS_ONLN)");
      }
    } else if (mrb_type(w) == MRB_TT_FIXNUM) {
      worker = mrb_fixnum(w);
    } else {
      mrb_raisef(mrb, E_RUNTIME_ERROR, "invalid worker parmeter: %S", w);
    }
    if (worker > MRB_HTTP2_WORKER_MAX) {
      mrb_raisef(mrb, E_RUNTIME_ERROR, "invalid worker parameter: "
          "%S > MRB_HTTP2_WORKER_MAX(%S)", mrb_fixnum_value(worker),
          mrb_fixnum_value(MRB_HTTP2_WORKER_MAX));
    }
  } else {
    worker = 0;
  }

#if !defined(__linux__) || !defined(SO_REUSEPORT)
  worker = 0;
#endif
  return worker;
}
Ejemplo n.º 6
0
static mrb_value
mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base)
{
  char buf[66], *b = buf + sizeof buf;
  mrb_int num = mrb_fixnum(x);
  uint64_t val = (uint64_t)num;
  char d;

  if (base != 2) {
    mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid radix %S", mrb_fixnum_value(base));
  }
  if (val == 0) {
    return mrb_str_new_lit(mrb, "0");
  }
  *--b = '\0';
  do {
    *--b = mrb_digitmap[(int)(val % base)];
  } while (val /= base);

  if (num < 0) {
    b = remove_sign_bits(b, base);
    switch (base) {
    case 16: d = 'f'; break;
    case 8:  d = '7'; break;
    case 2:  d = '1'; break;
    default: d = 0;   break;
    }

    if (d && *b != d) {
      *--b = d;
    }
  }

  return mrb_str_new_cstr(mrb, b);
}
Ejemplo n.º 7
0
Archivo: proc.c Proyecto: kmasa/mruby
static mrb_value
mrb_proc_inspect(mrb_state *mrb, mrb_value self)
{
    struct RProc *p = mrb_proc_ptr(self);
    mrb_value str = mrb_str_new_lit(mrb, "#<Proc:");
    mrb_str_concat(mrb, str, mrb_ptr_to_str(mrb, mrb_cptr(self)));

    if (!MRB_PROC_CFUNC_P(p)) {
        mrb_irep *irep = p->body.irep;
        mrb_str_cat_lit(mrb, str, "@");

        if (irep->filename) {
            mrb_str_cat_cstr(mrb, str, irep->filename);
        }
        else {
            mrb_str_cat_lit(mrb, str, "-");
        }
        mrb_str_cat_lit(mrb, str, ":");

        if (irep->lines) {
            mrb_str_append(mrb, str, mrb_fixnum_value(*irep->lines));
        }
        else {
            mrb_str_cat_lit(mrb, str, "-");
        }
    }

    if (MRB_PROC_STRICT_P(p)) {
        mrb_str_cat_lit(mrb, str, " (lambda)");
    }

    mrb_str_cat_lit(mrb, str, ">");
    return str;
}
Ejemplo n.º 8
0
static mrb_value
flo_to_s(mrb_state *mrb, mrb_value flt)
{
  if (isnan(mrb_float(flt))) {
    return mrb_str_new_lit(mrb, "NaN");
  }
  return mrb_float_to_str(mrb, flt, MRB_FLO_TO_STR_FMT);
}
Ejemplo n.º 9
0
void
mrb_show_version(mrb_state *mrb)
{
  mrb_value msg;

  msg = mrb_const_get(mrb, mrb_obj_value(mrb->object_class), mrb_intern_lit(mrb, "MRUBY_DESCRIPTION"));
  printstr(mrb, msg);
  printstr(mrb, mrb_str_new_lit(mrb, "\n"));
}
Ejemplo n.º 10
0
Archivo: hash.c Proyecto: denji/mruby
static mrb_value
mrb_hash_inspect(mrb_state *mrb, mrb_value hash)
{
  khash_t(ht) *h = RHASH_TBL(hash);

  if (!h || kh_size(h) == 0)
    return mrb_str_new_lit(mrb, "{}");
  return inspect_hash(mrb, hash, 0);
}
Ejemplo n.º 11
0
void
mrb_show_copyright(mrb_state *mrb)
{
  mrb_value msg;

  msg = mrb_const_get(mrb, mrb_obj_value(mrb->object_class), mrb_intern_lit(mrb, "MRUBY_COPYRIGHT"));
  printstr(mrb, msg);
  printstr(mrb, mrb_str_new_lit(mrb, "\n"));
}
Ejemplo n.º 12
0
Archivo: gc.c Proyecto: anehing/mruby
void
test_add_gray_list(void)
{
  mrb_state *mrb = mrb_open();
  struct RBasic *obj1, *obj2;

  puts("test_add_gray_list");
  change_gen_gc_mode(mrb, FALSE);
  mrb_assert(mrb->gray_list == NULL);
  obj1 = mrb_basic_ptr(mrb_str_new_lit(mrb, "test"));
  add_gray_list(mrb, obj1);
  mrb_assert(mrb->gray_list == obj1);
  mrb_assert(is_gray(obj1));

  obj2 = mrb_basic_ptr(mrb_str_new_lit(mrb, "test"));
  add_gray_list(mrb, obj2);
  mrb_assert(mrb->gray_list == obj2);
  mrb_assert(mrb->gray_list->gcnext == obj1);
  mrb_assert(is_gray(obj2));

  mrb_close(mrb);
}
Ejemplo n.º 13
0
Archivo: struct.c Proyecto: nyanp/mruby
static mrb_value
inspect_struct(mrb_state *mrb, mrb_value s, int recur)
{
  const char *cn = mrb_class_name(mrb, mrb_obj_class(mrb, s));
  mrb_value members, str = mrb_str_new_lit(mrb, "#<struct ");
  mrb_value *ptr, *ptr_members;
  mrb_int i, len;

  if (cn) {
    mrb_str_append(mrb, str, mrb_str_new_cstr(mrb, cn));
  }
  if (recur) {
    return mrb_str_cat_lit(mrb, str, ":...>");
  }

  members = mrb_struct_members(mrb, s);
  ptr_members = RARRAY_PTR(members);
  ptr = RSTRUCT_PTR(s);
  len = RSTRUCT_LEN(s);
  for (i=0; i<len; i++) {
    mrb_value slot;
    mrb_sym id;

    if (i > 0) {
      mrb_str_cat_lit(mrb, str, ", ");
    }
    else if (cn) {
      mrb_str_cat_lit(mrb, str, " ");
    }
    slot = ptr_members[i];
    id = mrb_symbol(slot);
    if (mrb_is_local_id(id) || mrb_is_const_id(id)) {
      const char *name;
      mrb_int len;

      name = mrb_sym2name_len(mrb, id, &len);
      mrb_str_append(mrb, str, mrb_str_new(mrb, name, len));
    }
    else {
      mrb_str_append(mrb, str, mrb_inspect(mrb, slot));
    }
    mrb_str_cat_lit(mrb, str, "=");
    mrb_str_append(mrb, str, mrb_inspect(mrb, ptr[i]));
  }
  mrb_str_cat_lit(mrb, str, ">");

  return str;
}
Ejemplo n.º 14
0
int main(void)
{
  mrb_value response;
  mrb_state *mrb = mrb_open();
  mrbc_context *ctx = mrbc_context_new(mrb);

  // URL into instance variable "@url" of main on same ctx
  mrb_iv_set(mrb, mrb_top_self(mrb), mrb_intern_lit(mrb, "@url"),
      mrb_str_new_lit(mrb, "https://127.0.0.1:8080/index.html"));
  response = mrb_load_string_cxt(mrb, request, ctx);
  mrb_funcall(mrb, mrb_top_self(mrb), "pp", 1, response);

  mrbc_context_free(mrb, ctx);
  mrb_close(mrb);
  return 0;
}
Ejemplo n.º 15
0
void
mrb_init_version(mrb_state* mrb)
{
  mrb_value mruby_version = mrb_str_new_lit(mrb, MRUBY_VERSION);

  mrb_define_global_const(mrb, "RUBY_VERSION", mrb_str_new_lit(mrb, MRUBY_RUBY_VERSION));
  mrb_define_global_const(mrb, "RUBY_ENGINE", mrb_str_new_lit(mrb, MRUBY_RUBY_ENGINE));
  mrb_define_global_const(mrb, "RUBY_ENGINE_VERSION", mruby_version);
  mrb_define_global_const(mrb, "MRUBY_VERSION", mrb_str_new_lit(mrb, MRUBY_VERSION));
  mrb_define_global_const(mrb, "MRUBY_RELEASE_NO", mrb_fixnum_value(MRUBY_RELEASE_NO));
  mrb_define_global_const(mrb, "MRUBY_RELEASE_DATE", mrb_str_new_lit(mrb, MRUBY_RELEASE_DATE));
  mrb_define_global_const(mrb, "MRUBY_DESCRIPTION", mrb_str_new_lit(mrb, MRUBY_DESCRIPTION));
  mrb_define_global_const(mrb, "MRUBY_COPYRIGHT", mrb_str_new_lit(mrb, MRUBY_COPYRIGHT));
}
Ejemplo n.º 16
0
                       s.run                                  \n\
";

int main(void)
{
  mrb_state *mrb = mrb_open();
  mrbc_context *ctx = mrbc_context_new(mrb);

  // 8080 into instance variable "@port" of main on same ctx
  mrb_iv_set(mrb, mrb_top_self(mrb), mrb_intern_lit(mrb, "@port"),
      mrb_fixnum_value(8080));
  mrb_iv_set(mrb, mrb_top_self(mrb), mrb_intern_lit(mrb, "@echo_content"),
      mrb_str_new_lit(mrb, "world from cb."));
  mrb_load_string_cxt(mrb, config, ctx);
  mrb_load_string_cxt(mrb, response, ctx);
  mrb_load_string_cxt(mrb, run, ctx);

  mrbc_context_free(mrb, ctx);
  mrb_close(mrb);
  return 0;
}
Ejemplo n.º 17
0
static mrb_value ngx_mrb_get_nginx_version(mrb_state *mrb, mrb_value self)
{
  return mrb_str_new_lit(mrb, NGINX_VERSION);
}
Ejemplo n.º 18
0
Archivo: gc.c Proyecto: anehing/mruby
void
test_mrb_field_write_barrier(void)
{
  mrb_state *mrb = mrb_open();
  struct RBasic *obj, *value;

  puts("test_mrb_field_write_barrier");
  mrb->is_generational_gc_mode = FALSE;
  obj = mrb_basic_ptr(mrb_ary_new(mrb));
  value = mrb_basic_ptr(mrb_str_new_lit(mrb, "value"));
  paint_black(obj);
  paint_partial_white(mrb,value);


  puts("  in GC_STATE_MARK");
  mrb->gc_state = GC_STATE_MARK;
  mrb_field_write_barrier(mrb, obj, value);

  mrb_assert(is_gray(value));


  puts("  in GC_STATE_SWEEP");
  paint_partial_white(mrb,value);
  mrb->gc_state = GC_STATE_SWEEP;
  mrb_field_write_barrier(mrb, obj, value);

  mrb_assert(obj->color & mrb->current_white_part);
  mrb_assert(value->color & mrb->current_white_part);


  puts("  fail with black");
  mrb->gc_state = GC_STATE_MARK;
  paint_white(obj);
  paint_partial_white(mrb,value);
  mrb_field_write_barrier(mrb, obj, value);

  mrb_assert(obj->color & mrb->current_white_part);


  puts("  fail with gray");
  mrb->gc_state = GC_STATE_MARK;
  paint_black(obj);
  paint_gray(value);
  mrb_field_write_barrier(mrb, obj, value);

  mrb_assert(is_gray(value));


  {
    puts("test_mrb_field_write_barrier_value");
    obj = mrb_basic_ptr(mrb_ary_new(mrb));
    mrb_value value = mrb_str_new_lit(mrb, "value");
    paint_black(obj);
    paint_partial_white(mrb, mrb_basic_ptr(value));

    mrb->gc_state = GC_STATE_MARK;
    mrb_field_write_barrier_value(mrb, obj, value);

    mrb_assert(is_gray(mrb_basic_ptr(value)));
  }

  mrb_close(mrb);
}
Ejemplo n.º 19
0
static mrb_value ngx_http_mruby_get_nginx_configure(mrb_state *mrb, mrb_value self)
{
  return mrb_str_new_lit(mrb, NGX_CONFIGURE);
}
Ejemplo n.º 20
0
/*
 *  call-seq:
 *     string.succ    ->  string
 *
 *  Returns next sequence of the string;
 *
 *     a = "abc"
 *     a.succ    #=> "abd"
 */
static mrb_value
mrb_str_succ_bang(mrb_state *mrb, mrb_value self)
{
  mrb_value result;
  unsigned char *p, *e, *b, *t;
  const char *prepend;
  struct RString *s = mrb_str_ptr(self);
  size_t l;

  if (RSTRING_LEN(self) == 0)
    return self;

  mrb_str_modify(mrb, s);
  l = RSTRING_LEN(self);
  b = p = (unsigned char*) RSTRING_PTR(self);
  t = e = p + l;
  *(e--) = 0;

  // find trailing ascii/number
  while (e >= b) {
    if (ISALNUM(*e))
      break;
    e--;
  }
  if (e < b) {
    e = p + l - 1;
    result = mrb_str_new_lit(mrb, "");
  } else {
    // find leading letter of the ascii/number
    b = e;
    while (b > p) {
      if (!ISALNUM(*b) || (ISALNUM(*b) && *b != '9' && *b != 'z' && *b != 'Z'))
        break;
      b--;
    }
    if (!ISALNUM(*b))
      b++;
    result = mrb_str_new(mrb, (char*) p, b - p);
  }

  while (e >= b) {
    if (!ISALNUM(*e)) {
      if (*e == 0xff) {
        mrb_str_cat_lit(mrb, result, "\x01");
        (*e) = 0;
      } else
        (*e)++;
      break;
    }
    prepend = NULL;
    if (*e == '9') {
      if (e == b) prepend = "1";
      *e = '0';
    } else if (*e == 'z') {
      if (e == b) prepend = "a";
      *e = 'a';
    } else if (*e == 'Z') {
      if (e == b) prepend = "A";
      *e = 'A';
    } else {
      (*e)++;
      break;
    }
    if (prepend) mrb_str_cat_cstr(mrb, result, prepend);
    e--;
  }
  result = mrb_str_cat(mrb, result, (char*) b, t - b);
  l = RSTRING_LEN(result);
  mrb_str_resize(mrb, self, l);
  memcpy(RSTRING_PTR(self), RSTRING_PTR(result), l);
  return self;
}
Ejemplo n.º 21
0
static mrb_value
inspect_main(mrb_state *mrb, mrb_value mod)
{
  return mrb_str_new_lit(mrb, "main");
}
Ejemplo n.º 22
0
static mrb_value ngx_mrb_echo(mrb_state *mrb, mrb_value self)
{
  mrb_value argv;
  ngx_buf_t *b;
  ngx_mrb_rputs_chain_list_t *chain;
  u_char *str;
  ngx_str_t ns;

  ngx_http_request_t *r = ngx_mrb_get_request();
  ngx_http_mruby_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_mruby_module);

  mrb_get_args(mrb, "o", &argv);

  if (mrb_type(argv) != MRB_TT_STRING) {
    argv = mrb_funcall(mrb, argv, "to_s", 0, NULL);
  }

  ns.data = (u_char *)RSTRING_PTR(mrb_str_plus(mrb, argv, mrb_str_new_lit(mrb, "\n")));
  ns.len = RSTRING_LEN(argv) + sizeof("\n") - 1;
  if (ns.len == 0) {
    return self;
  }

  if (ctx->rputs_chain == NULL) {
    chain = ngx_pcalloc(r->pool, sizeof(ngx_mrb_rputs_chain_list_t));
    if (chain == NULL) {
      mrb_raise(mrb, E_RUNTIME_ERROR, "failed to allocate memory");
    }
    chain->out = ngx_alloc_chain_link(r->pool);
    if (chain->out == NULL) {
      mrb_raise(mrb, E_RUNTIME_ERROR, "failed to allocate memory");
    }
    chain->last = &chain->out;
  } else {
    chain = ctx->rputs_chain;
    (*chain->last)->next = ngx_alloc_chain_link(r->pool);
    if ((*chain->last)->next == NULL) {
      mrb_raise(mrb, E_RUNTIME_ERROR, "failed to allocate memory");
    }
    chain->last = &(*chain->last)->next;
  }
  b = ngx_calloc_buf(r->pool);
  if (b == NULL) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "failed to allocate memory");
  }
  (*chain->last)->buf = b;
  (*chain->last)->next = NULL;

  str = ngx_pstrdup(r->pool, &ns);
  if (str == NULL) {
    mrb_raise(mrb, E_RUNTIME_ERROR, "failed to allocate memory");
  }
  str[ns.len] = '\0';
  (*chain->last)->buf->pos = str;
  (*chain->last)->buf->last = str + ns.len;
  (*chain->last)->buf->memory = 1;
  ctx->rputs_chain = chain;
  ngx_http_set_ctx(r, ctx, ngx_http_mruby_module);

  if (r->headers_out.content_length_n == -1) {
    r->headers_out.content_length_n += ns.len + 1;
  } else {
    r->headers_out.content_length_n += ns.len;
  }

  return self;
}
Ejemplo n.º 23
0
int value_to_node(mrb_state *mrb,
  yaml_document_t *document, mrb_value value)
{
  int node;

  switch (mrb_type(value))
  {
    case MRB_TT_ARRAY:
    {
      mrb_int len = mrb_ary_len(mrb, value);
      mrb_int i;
      int ai = mrb_gc_arena_save(mrb);

      node = yaml_document_add_sequence(document, NULL,
        YAML_ANY_SEQUENCE_STYLE);

      for (i = 0; i < len; i++)
      {
        mrb_value child = mrb_ary_ref(mrb, value, i);
        int child_node = value_to_node(mrb, document, child);

        /* Add the child to the sequence */
        yaml_document_append_sequence_item(document, node, child_node);
        mrb_gc_arena_restore(mrb, ai);
      }

      break;
    }

    case MRB_TT_HASH:
    {
      /* Iterating a list of keys is slow, but it only
       * requires use of the interface defined in `hash.h`.
       */

      mrb_value keys = mrb_hash_keys(mrb, value);
      mrb_int len = mrb_ary_len(mrb, keys);
      mrb_int i;
      int ai = mrb_gc_arena_save(mrb);

      node = yaml_document_add_mapping(document, NULL,
        YAML_ANY_MAPPING_STYLE);

      for (i = 0; i < len; i++)
      {
        mrb_value key = mrb_ary_ref(mrb, keys, i);
        mrb_value child = mrb_hash_get(mrb, value, key);

        int key_node = value_to_node(mrb, document, key);
        int child_node = value_to_node(mrb, document, child);

        /* Add the key/value pair to the mapping */
        yaml_document_append_mapping_pair(document, node,
          key_node, child_node);
        mrb_gc_arena_restore(mrb, ai);
      }

      break;
    }

    default:
    {
      if (mrb_nil_p(value)) {
        /* http://yaml.org/type/null.html
           Canonical form */
        value = mrb_str_new_lit(mrb, "~");
      } else {
        /* Equivalent to `obj = obj#to_s` */
        value = mrb_obj_as_string(mrb, value);
      }
      /* Fallthrough */
    }

    case MRB_TT_STRING:
    {
      yaml_scalar_style_t style = YAML_ANY_SCALAR_STYLE;
      if (RSTRING_LEN(value) == 0) {
        /* If the String is empty, it may be reloaded as a nil instead of an
         * empty string, to avoid that place a quoted string instead */
        style = YAML_SINGLE_QUOTED_SCALAR_STYLE;
      }
      yaml_char_t *value_chars = (unsigned char *) RSTRING_PTR(value);
      node = yaml_document_add_scalar(document, NULL,
        value_chars, RSTRING_LEN(value), style);
      break;
    }
  }

  return node;
}
Ejemplo n.º 24
0
static mrb_value
false_to_s(mrb_state *mrb, mrb_value obj)
{
  return mrb_str_new_lit(mrb, "false");
}
Ejemplo n.º 25
0
static mrb_value
nil_inspect(mrb_state *mrb, mrb_value obj)
{
  return mrb_str_new_lit(mrb, "nil");
}
Ejemplo n.º 26
0
static mrb_value
true_to_s(mrb_state *mrb, mrb_value obj)
{
  return mrb_str_new_lit(mrb, "true");
}
Ejemplo n.º 27
0
static mrb_value ngx_mrb_server_name(mrb_state *mrb, mrb_value self)
{
  return mrb_str_new_lit(mrb, NGINX_VAR);
}
Ejemplo n.º 28
0
static mrb_value
mrb_flo_to_str(mrb_state *mrb, mrb_float flo)
{
  double n = (double)flo;
  int max_digits = FLO_MAX_DIGITS;

  if (isnan(n)) {
    return mrb_str_new_lit(mrb, "NaN");
  }
  else if (isinf(n)) {
    if (n < 0) {
      return mrb_str_new_lit(mrb, "-inf");
    }
    else {
      return mrb_str_new_lit(mrb, "inf");
    }
  }
  else {
    int digit;
    int m = 0;
    int exp;
    mrb_bool e = FALSE;
    char s[48];
    char *c = &s[0];
    int length = 0;

    if (signbit(n)) {
      n = -n;
      *(c++) = '-';
    }

    if (n != 0.0) {
      if (n > 1.0) {
        exp = (int)floor(log10(n));
      }
      else {
        exp = (int)-ceil(-log10(n));
      }
    }
    else {
      exp = 0;
    }

    /* preserve significands */
    if (exp < 0) {
      int i, beg = -1, end = 0;
      double f = n;
      double fd = 0;
      for (i = 0; i < FLO_MAX_DIGITS; ++i) {
        f = (f - fd) * 10.0;
        fd = floor(f + FLO_EPSILON);
        if (fd != 0) {
          if (beg < 0) beg = i;
          end = i + 1;
        }
      }
      if (beg >= 0) length = end - beg;
      if (length > FLO_MAX_SIGN_LENGTH) length = FLO_MAX_SIGN_LENGTH;
    }

    if (abs(exp) + length >= FLO_MAX_DIGITS) {
      /* exponent representation */
      e = TRUE;
      n = n / pow(10.0, exp);
      if (isinf(n)) {
        if (s < c) {            /* s[0] == '-' */
          return mrb_str_new_lit(mrb, "-0.0");
        }
        else {
          return mrb_str_new_lit(mrb, "0.0");
        }
      }
    }
    else {
      /* un-exponent (normal) representation */
      if (exp > 0) {
        m = exp;
      }
    }

    /* puts digits */
    while (max_digits >= 0) {
      double weight = (m < 0) ? 0.0 : pow(10.0, m);
      double fdigit = (m < 0) ? n * 10.0 : n / weight;

      if (fdigit < 0) fdigit = n = 0;
      if (m < -1 && fdigit < FLO_EPSILON) {
        if (e || exp > 0 || m <= -abs(exp)) {
          break;
        }
      }
      digit = (int)floor(fdigit + FLO_EPSILON);
      if (m == 0 && digit > 9) {
        n /= 10.0;
        exp++;
        continue;
      }
      *(c++) = '0' + digit;
      n = (m < 0) ? n * 10.0 - digit : n - (digit * weight);
      max_digits--;
      if (m-- == 0) {
        *(c++) = '.';
      }
    }
    if (c[-1] == '0') {
      while (&s[0] < c && c[-1] == '0') {
        c--;
      }
      c++;
    }

    if (e) {
      *(c++) = 'e';
      if (exp > 0) {
        *(c++) = '+';
      }
      else {
        *(c++) = '-';
        exp = -exp;
      }

      if (exp >= 100) {
        *(c++) = '0' + exp / 100;
        exp -= exp / 100 * 100;
      }

      *(c++) = '0' + exp / 10;
      *(c++) = '0' + exp % 10;
    }

    *c = '\0';

    return mrb_str_new(mrb, &s[0], c - &s[0]);
  }
}
Ejemplo n.º 29
0
static mrb_value ngx_mrb_get_ngx_mruby_version(mrb_state *mrb, mrb_value self)
{
  return mrb_str_new_lit(mrb, MODULE_VERSION);
}
Ejemplo n.º 30
0
static mrb_value ngx_mrb_get_ngx_mruby_name(mrb_state *mrb, mrb_value self)
{
  return mrb_str_new_lit(mrb, MODULE_NAME);
}