コード例 #1
0
ファイル: group.cpp プロジェクト: cifarelli/mruby-fltk
// FLTK::Group#resizable
static mrb_value
mrb_fltk_group_resizable_get_method( mrb_state *mrb, mrb_value self ) {
  CONTEXT_SETUP( widget );

  struct RClass *mrb_fltk_class = mrb_class_get( mrb, "FLTK" );
  struct RClass *mrb_fltk_widget_class = mrb_class_ptr( mrb_const_get( mrb, mrb_obj_value( mrb_fltk_class ), mrb_intern_cstr( mrb, "Widget" ) ) );

  mrb_value args[1];

  args[0] = mrb_obj_value(
    Data_Wrap_Struct( mrb, mrb->object_class, &fltk_widget_type, (void *)( (Fl_Group *)context->fl_instance )->resizable() ) );

  return mrb_class_new_instance( mrb, 1, args, mrb_fltk_widget_class );
}
コード例 #2
0
ファイル: mrb_http.c プロジェクト: masuidrive/mruby-http
static mrb_value
mrb_http_parser_parse_url(mrb_state *mrb, mrb_value self)
{
  mrb_value c;
  mrb_value arg_data;
  struct http_parser_url handle = {0};
  struct RClass* _class_http_url;

  mrb_get_args(mrb, "S", &arg_data);

  if (http_parser_parse_url(RSTRING_PTR(arg_data), RSTRING_LEN(arg_data), FALSE, &handle)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid URL");
  }

  _class_http_url = mrb_class_get(mrb, "HTTP");
  c = mrb_class_new_instance(mrb, 0, NULL, _class_http_url);

  if (handle.field_set & (1<<UF_SCHEMA)) {
    OBJECT_SET(mrb, c, "schema", mrb_str_substr(mrb, arg_data, handle.field_data[UF_SCHEMA].off, handle.field_data[UF_SCHEMA].len));
  }
  if (handle.field_set & (1<<UF_HOST)) {
    OBJECT_SET(mrb, c, "host", mrb_str_substr(mrb, arg_data, handle.field_data[UF_HOST].off, handle.field_data[UF_HOST].len));
  }
  if (handle.field_set & (1<<UF_HOST)) {
    OBJECT_SET(mrb, c, "host", mrb_str_substr(mrb, arg_data, handle.field_data[UF_HOST].off, handle.field_data[UF_HOST].len));
  }
  if (handle.field_set & (1<<UF_PORT)) {
    OBJECT_SET(mrb, c, "port", mrb_fixnum_value(handle.port));
  } else {
    if (handle.field_set & (1<<UF_SCHEMA)) {
      mrb_value schema = mrb_str_substr(mrb, arg_data, handle.field_data[UF_SCHEMA].off, handle.field_data[UF_SCHEMA].len);
      if (!mrb_nil_p(schema) && !strcmp("https", (char*) RSTRING_PTR(schema))) {
        OBJECT_SET(mrb, c, "port", mrb_fixnum_value(443));
      }
    }
  }
  if (handle.field_set & (1<<UF_PATH)) {
    OBJECT_SET(mrb, c, "path", mrb_str_substr(mrb, arg_data, handle.field_data[UF_PATH].off, handle.field_data[UF_PATH].len));
  }
  if (handle.field_set & (1<<UF_QUERY)) {
    OBJECT_SET(mrb, c, "query", mrb_str_substr(mrb, arg_data, handle.field_data[UF_QUERY].off, handle.field_data[UF_QUERY].len));
  }
  if (handle.field_set & (1<<UF_FRAGMENT)) {
    OBJECT_SET(mrb, c, "fragment", mrb_str_substr(mrb, arg_data, handle.field_data[UF_FRAGMENT].off, handle.field_data[UF_FRAGMENT].len));
  }

  return c;
}
コード例 #3
0
ファイル: error.c プロジェクト: Zyxwvu/mruby
void
mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...)
{
  mrb_value exc, argv[2];
  va_list args;
  char buf[256];

  va_start(args, fmt);
  //argv[0] = mrb_vsprintf(fmt, args);
  vsnprintf(buf, 256, fmt, args);
  argv[0] = mrb_str_new(mrb, buf, strlen(buf));
  va_end(args);

  argv[1] = mrb_str_new_cstr(mrb, mrb_sym2name(mrb, id));
  exc = mrb_class_new_instance(mrb, 2, argv, E_NAME_ERROR);
  mrb_exc_raise(mrb, exc);
}
コード例 #4
0
ファイル: mrb_http.c プロジェクト: rystyle/mruby-http
static int
parser_settings_on_message_complete(http_parser* parser)
{
    mrb_value proc;
    mrb_value c;
    mrb_http_parser_context *context = (mrb_http_parser_context*) parser->data;
    mrb_state* mrb = context->mrb;

    proc = mrb_iv_get(context->mrb, context->instance, mrb_intern(context->mrb, "complete_cb"));

    c = mrb_class_new_instance(mrb, 0, NULL, _class_http_request);
    mrb_iv_set(mrb, c, mrb_intern(mrb, "parser_context"), mrb_obj_value(
                   Data_Wrap_Struct(mrb, mrb->object_class,
                                    NULL, (void*) context)));
    mrb_yield(context->mrb, proc, c);

    return 0;
}
コード例 #5
0
ファイル: error.c プロジェクト: Blorgus/mruby
void
mrb_name_error(mrb_state *mrb, mrb_sym id, const char *fmt, ...)
{
  mrb_value exc, argv[2];
  va_list args;
  char buf[256];
  int n;

  va_start(args, fmt);
  n = vsnprintf(buf, 256, fmt, args);
  va_end(args);
  if (n < 0) {
    n = 0;
  }
  argv[0] = mrb_str_new(mrb, buf, n);
  argv[1] = mrb_symbol_value(id); /* ignore now */
  exc = mrb_class_new_instance(mrb, 1, argv, E_NAME_ERROR);
  mrb_exc_raise(mrb, exc);
}
コード例 #6
0
// using from ngx_http_mruby_connection.c and ngx_http_mruby_server.c
mrb_value ngx_mrb_get_request_var(mrb_state *mrb, mrb_value self)
{
    const char               *iv_var_str         = "@iv_var";
    mrb_value                iv_var;
    struct RClass            *class_var, *ngx_class;

    iv_var = mrb_iv_get(mrb, self, mrb_intern(mrb, iv_var_str));
    if (mrb_nil_p(iv_var)) {
        // get class from Nginx::Var
        ngx_class = mrb_class_get(mrb, "Nginx");
        class_var = (struct RClass*)mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(ngx_class), mrb_intern_cstr(mrb, "Var")));
        // initialize a Var instance
        iv_var = mrb_class_new_instance(mrb, 0, 0, class_var);
        // save Var, avoid multi initialize
        mrb_iv_set(mrb, self, mrb_intern(mrb, iv_var_str), iv_var);
    }

    return iv_var;
}
コード例 #7
0
ファイル: struct.c プロジェクト: Kazu-zamasu/mruby_efi_shell
mrb_value
mrb_struct_new(mrb_state *mrb, struct RClass *klass, ...)
{
    mrb_value tmpargs[N_REF_FUNC], *mem = tmpargs;
    int size, i;
    va_list args;

    size = mrb_long2int(num_members(mrb, klass));
    if (size > numberof(tmpargs)) {
      tmpargs[0] = mrb_ary_tmp_new(mrb, size);
      mem = RARRAY_PTR(tmpargs[0]);
    }
    va_start(args, klass);
    for (i=0; i<size; i++) {
      mem[i] = va_arg(args, mrb_value);
    }
    va_end(args);

    return mrb_class_new_instance(mrb, size, mem, klass);
}
コード例 #8
0
ファイル: mrb_http.c プロジェクト: sogabe/mruby-http
static mrb_value
mrb_http_parser_parse_url(mrb_state *mrb, mrb_value self)
{
  mrb_value c;
  mrb_value arg_data;
  struct http_parser_url *context = NULL;

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

  context = (struct http_parser_url*) malloc(sizeof(struct http_parser_url));
  memset(context, 0, sizeof(struct http_parser_url));
  http_parser_parse_url(RSTRING_PTR(arg_data), RSTRING_LEN(arg_data), FALSE, context);

  c = mrb_class_new_instance(mrb, 0, NULL, _class_http_url);
  mrb_iv_set(mrb, c, mrb_intern(mrb, "context"), mrb_obj_value(
    Data_Wrap_Struct(mrb, mrb->object_class,
    &http_url_type, (void*) context)));
  mrb_iv_set(mrb, c, mrb_intern(mrb, "buf"), arg_data);

  return c;
}
コード例 #9
0
ファイル: mrb_http.c プロジェクト: sogabe/mruby-http
static int
parser_settings_on_message_complete(http_parser* parser)
{
  mrb_value c;
  mrb_http_parser_context *context = (mrb_http_parser_context*) parser->data;
  mrb_http_parser_context *new_context;
  mrb_state* mrb = context->mrb;
  mrb_value args[1];

  c = mrb_class_new_instance(mrb, 0, NULL, _class_http_request);
  new_context = (mrb_http_parser_context*) malloc(sizeof(mrb_http_parser_context));
  memcpy(new_context, context, sizeof(mrb_http_parser_context));
  mrb_iv_set(mrb, c, mrb_intern(mrb, "context"), mrb_obj_value(
    Data_Wrap_Struct(mrb, mrb->object_class,
    &http_parser_context_type, (void*) new_context)));
  args[0] = c;
  mrb_yield_argv(context->mrb, context->proc, 1, args);
  PARSER_SET(context, "headers", mrb_nil_value());
  PARSER_SET(context, "last_header_field", mrb_nil_value());
  PARSER_SET(context, "last_header_value", mrb_nil_value());
  PARSER_SET(context, "buf", mrb_nil_value());

  return 0;
}
コード例 #10
0
ファイル: main.cpp プロジェクト: kyab/mruby-thread-freertos
//utility to make instance of Blinker
static mrb_value make_blinker_obj(int pin, int interval_ms) 
{
    RClass *blinker_class = mrb_class_get(g_mrb, "Blinker");
    if (g_mrb->exc){
        p(g_mrb, mrb_obj_value(g_mrb->exc));
        Serial2.println("failed to get Blinker class");
        g_mrb->exc = 0;
        return mrb_nil_value();
    }

    mrb_value args[2];
    args[0] = mrb_fixnum_value(pin);
    args[1] = mrb_fixnum_value(interval_ms);

    mrb_value blinker_obj = mrb_class_new_instance(g_mrb, 2, args, blinker_class);
    if (g_mrb->exc){
        p(g_mrb, mrb_obj_value(g_mrb->exc));
        Serial2.println("failed to create new Blinker instance");
        g_mrb->exc = 0;
        return mrb_nil_value();
    }

    return blinker_obj;
}
コード例 #11
0
ファイル: mrb_http.c プロジェクト: masuidrive/mruby-http
static mrb_value
_http_parser_parse(mrb_state *mrb, mrb_value self, int type)
{
  mrb_value arg_data = mrb_nil_value();
  mrb_value value_context;
  mrb_http_parser_context* context;
  mrb_value b = mrb_nil_value();
  struct RClass* _class_http;
  struct RClass* clazz;
  char* data;
  size_t len;
  char* eol;
  size_t done;

  value_context = mrb_iv_get(mrb, self, mrb_intern(mrb, "context"));
  Data_Get_Struct(mrb, value_context, &http_parser_context_type, context);
  if (!context) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }

  mrb_get_args(mrb, "|&o", &b, &arg_data);
  if (mrb_nil_p(arg_data)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }
  context->parser.data = context;

  _class_http = mrb_class_get(mrb, "HTTP");
  if (type == HTTP_REQUEST) {
    clazz = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(_class_http), mrb_intern(mrb, "Request")));
    context->instance = mrb_class_new_instance(mrb, 0, NULL, clazz);
  } else {
    clazz = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(_class_http), mrb_intern(mrb, "Response")));
    context->instance = mrb_class_new_instance(mrb, 0, NULL, clazz);
  }
  context->was_header_value = TRUE;

  http_parser_init(&context->parser, type);

  context->type = type;
  context->settings.on_url = parser_settings_on_url;
  context->settings.on_header_field = parser_settings_on_header_field;
  context->settings.on_header_value = parser_settings_on_header_value;
  context->settings.on_headers_complete = parser_settings_on_headers_complete;
  context->settings.on_body = parser_settings_on_body;
  context->settings.on_message_complete = parser_settings_on_message_complete;

  data = RSTRING_PTR(arg_data);
  len = RSTRING_LEN(arg_data);

  eol = strpbrk(data, "\r\n");
  if (eol) {
  }

RETRY:
  if (len > 10 && (!strncmp(data+9, "200 Connection established\r\n", 28) ||
      !strncmp(data+9, "100 Continue\r\n", 14) || *(data+9) == '3')) {
    char* next = strstr(data, "\r\n\r\n");
    if (next) {
      len -= (next + 4 - data);
      data = next + 4;
      goto RETRY;
    }
  }

  done = http_parser_execute(&context->parser, &context->settings, data, len);
  if (done < len) {
    OBJECT_SET(mrb, context->instance, "body", mrb_str_new(mrb, data + done, len - done));
  }

  if (!mrb_nil_p(b)) {
    mrb_value args[1];
    args[0] = context->instance;
    mrb_yield_argv(mrb, b, 1, args);
    return mrb_nil_value();
  }
  return context->instance;
}
コード例 #12
0
ファイル: struct.c プロジェクト: Kazu-zamasu/mruby_efi_shell
mrb_value
mrb_struct_alloc(mrb_state *mrb, mrb_value klass, mrb_value values)
{
    return mrb_class_new_instance(mrb, RARRAY_LEN(values), RARRAY_PTR(values), mrb_class(mrb, klass));
}
コード例 #13
0
static mrb_value
mrb_sqlite3_database_execute(mrb_state *mrb, mrb_value self) {
  int argc = 0;
  mrb_value* argv = NULL;
  mrb_value b = mrb_nil_value();
  mrb_value value_context;
  mrb_sqlite3_database* db = NULL;
  mrb_value fields;
  int i, r, count;
  sqlite3_stmt* stmt = NULL;
  mrb_value args[2];
  mrb_value query;

  mrb_get_args(mrb, "&S*", &b, &query, &argv, &argc);

  value_context = mrb_iv_get(mrb, self, mrb_intern(mrb, "context"));
  db = NULL;
  Data_Get_Struct(mrb, value_context, &mrb_sqlite3_database_type, db);
  if (!db) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument");
  }

  r = sqlite3_prepare_v2(db->db, RSTRING_PTR(query), RSTRING_LEN(query),
    &stmt, NULL);
  if (r != SQLITE_OK) {
    if (stmt) {
      sqlite3_finalize(stmt);
      sqlite3_reset(stmt);
    }
    mrb_raise(mrb, E_RUNTIME_ERROR, sqlite3_errmsg(db->db));
  }
  if (!stmt) {
    return mrb_nil_value();
  }

  if (argc > 0) {
    const char* error = bind_values(mrb, db->db, stmt, argc, argv);
    if (error) {
      mrb_raise(mrb, E_ARGUMENT_ERROR, error);
    }
  }

  fields = mrb_ary_new(mrb);
  count = sqlite3_column_count(stmt);
  for (i = 0; i < count; i++) {
    const char* name = sqlite3_column_name(stmt, i);
    mrb_ary_push(mrb, fields, mrb_str_new_cstr(mrb, name));
  }

  if (mrb_nil_p(b)) {
    struct RClass* _class_sqlite3;
    struct RClass* _class_sqlite3_resultset;
    mrb_value c;
    mrb_sqlite3_resultset* rs = (mrb_sqlite3_resultset*)
      malloc(sizeof(mrb_sqlite3_resultset));
    if (!rs) {
      mrb_raise(mrb, E_RUNTIME_ERROR, "can't memory alloc");
    }
    memset(rs, 0, sizeof(mrb_sqlite3_resultset));
    rs->mrb = mrb;
    rs->stmt = stmt;
    _class_sqlite3 = mrb_class_get(mrb, "SQLite3");
    _class_sqlite3_resultset = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(_class_sqlite3), mrb_intern(mrb, "ResultSet")));
    c = mrb_class_new_instance(mrb, 0, NULL, _class_sqlite3_resultset);
    mrb_iv_set(mrb, c, mrb_intern(mrb, "context"), mrb_obj_value(
      Data_Wrap_Struct(mrb, mrb->object_class,
      &mrb_sqlite3_resultset_type, (void*) rs)));
    mrb_iv_set(mrb, c, mrb_intern(mrb, "fields"), fields);
    mrb_iv_set(mrb, c, mrb_intern(mrb, "db"), self);
    mrb_iv_set(mrb, c, mrb_intern(mrb, "eof"), mrb_false_value());
    return c;
  }
  while ((r = sqlite3_step(stmt)) == SQLITE_ROW) {
    int ai = mrb_gc_arena_save(mrb);
    args[0] = row_to_value(mrb, stmt);
    args[1] = fields;
    mrb_yield_argv(mrb, b, 2, args);
    mrb_gc_arena_restore(mrb, ai);
  }
  sqlite3_finalize(stmt);
  if (r != SQLITE_OK && r != SQLITE_DONE) {
    mrb_raise(mrb, E_RUNTIME_ERROR, sqlite3_errmsg(db->db));
  }
  return mrb_nil_value();
}
コード例 #14
0
ファイル: mrb_jpeg.c プロジェクト: keizo042/mruby-jpeg
static mrb_value
mrb_jpeg_decompress_common(mrb_state *mrb, mrb_value self, enum decompress_type dtype)
{
  struct jpeg_decompress_struct dinfo;
  struct jpeg_error_mgr jpeg_error;
  long scanline_size;
  void *jpeg_data;
  int row = 0;
  struct RClass* module_jpeg;
  struct RClass* class_jpeg_image;
  mrb_value ivar;

  mrb_value arg_config_hash = mrb_nil_value();
  mrb_value arg_data = mrb_nil_value();

  int argc = mrb_get_args(mrb, "S|H", &arg_data, &arg_config_hash);
  if (mrb_nil_p(arg_data) || mrb_type(arg_data) != MRB_TT_STRING) 
  {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "Invalid argument");
  }
  if(argc > 1 && mrb_type(arg_config_hash) != MRB_TT_HASH)
  {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "Invalid argument");
  }
  
  dinfo.client_data = mrb;

  dinfo.err = jpeg_std_error(&jpeg_error);
  dinfo.err->error_exit = jpeg_error_func;

  jpeg_create_decompress(&dinfo);

  if(dtype == FILE_SRC)
  {
    FILE *fp = fopen(RSTRING_PTR(arg_data), "r");
    if(fp == NULL)
    {
      jpeg_destroy_decompress(&dinfo);
      mrb_raise(mrb, E_RUNTIME_ERROR, "Could not open input file.");
    }
    jpeg_stdio_src(&dinfo, fp);
  }
  else
  {
    jpeg_memory_src(&dinfo, (const JOCTET *)RSTRING_PTR(arg_data), RSTRING_LEN(arg_data));
  }

  jpeg_read_header(&dinfo, 1); 

  if(!mrb_obj_equal(mrb, arg_config_hash, mrb_nil_value()) && HASH_TRUE(mrb, arg_config_hash, "force_grayscale"))
  {
    dinfo.output_components = 1;
  }
  else
  {
    if (dinfo.out_color_space == JCS_GRAYSCALE) 
    {
      dinfo.output_components = 1;
    }
    else 
    {
      dinfo.output_components = 3;
      dinfo.out_color_space = JCS_RGB;
    }
  }

  jpeg_start_decompress(&dinfo);

  scanline_size = dinfo.image_width * dinfo.output_components;
  jpeg_data = malloc(scanline_size * dinfo.image_height);

  for(row = 0; row < dinfo.image_height; row++)
  {
    JSAMPROW jpeg_row = (JSAMPROW)(jpeg_data + (scanline_size * row));
    jpeg_read_scanlines(&dinfo, &jpeg_row, 1);
  }

  module_jpeg = mrb_module_get(mrb, "JPEG");
  class_jpeg_image = mrb_class_ptr(mrb_const_get(mrb, mrb_obj_value(module_jpeg), mrb_intern_lit(mrb, "JPEGImage")));
  ivar = mrb_class_new_instance(mrb, 0, NULL, class_jpeg_image);
  mrb_iv_set(mrb, ivar, mrb_intern_lit(mrb, "data"), mrb_str_new(mrb, jpeg_data, scanline_size * dinfo.image_height));
  mrb_iv_set(mrb, ivar, mrb_intern_lit(mrb, "width"), mrb_fixnum_value(dinfo.image_width));
  mrb_iv_set(mrb, ivar, mrb_intern_lit(mrb, "height"), mrb_fixnum_value(dinfo.image_height));

  jpeg_finish_decompress(&dinfo);
  jpeg_destroy_decompress(&dinfo);

  return ivar;
}