Skip to content

Add a new mrb_fiber_new() with MRB_API#6097

Merged
matz merged 1 commit into
mruby:masterfrom
dearblue:fiber-new
Nov 7, 2023
Merged

Add a new mrb_fiber_new() with MRB_API#6097
matz merged 1 commit into
mruby:masterfrom
dearblue:fiber-new

Conversation

@dearblue

@dearblue dearblue commented Nov 5, 2023

Copy link
Copy Markdown
Contributor

No description provided.

mrb_fiber_new(mrb_state *mrb, const struct RProc *p)
{
struct RClass *c = mrb_class_get_id(mrb, MRB_SYM(Fiber));
if (MRB_INSTANCE_TT(c) != MRB_TT_FIBER) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When MRB_INSTANCE_TT(c) != MRB_TT_FIBER, MRB_OBJ_ALLOC will cause TypeError.
Do we really need this check?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I consider it necessary.

% cat 1.c
#include <mruby.h>

int
main(int argc, char *argv[])
{
  mrb_state *mrb = mrb_open();
  struct RFiber *o = MRB_OBJ_ALLOC(mrb, MRB_TT_FIBER, mrb->object_class);
  mrb_p(mrb, mrb_obj_value(o));
  mrb_close(mrb);

  return 0;
}

% $(bin/mruby-config --cc --cflags --ldflags --libs) 1.c && ./a.out
#<Object:0x823fcf720>

This is because the instance type tag of the Object class is MRB_TT_FALSE and the ttype parameter of mrb_obj_alloc() is rarely checked in this case.

mruby/src/gc.c

Lines 477 to 490 in 9e50d67

tt = MRB_INSTANCE_TT(cls);
if (tt != MRB_TT_FALSE &&
tt != MRB_TT_UNDEF &&
ttype != MRB_TT_SCLASS &&
ttype != MRB_TT_ICLASS &&
ttype != MRB_TT_ENV &&
ttype != MRB_TT_BIGINT &&
ttype != tt) {
mrb_raisef(mrb, E_TYPE_ERROR, "allocation failure of %C", cls);
}
}
if (ttype <= MRB_TT_FREE) {
mrb_raisef(mrb, E_TYPE_ERROR, "allocation failure of %C (type %d)", cls, (int)ttype);
}

Or is it better to take the same approach as Random?
9b9424f#diff-bf9e7692602cba54ba62bd71b76c439be9c9b71c6bb8d51ab996fc91cf1ee807

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to review the condition in mrb_obj_alloc().

Besides that, I don't think we should take Random approach. Random uses TT_ISTRUCT type that may be used by other classes. In contrast, TT_FIBER is only used by Fiber class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this will help, but I have an experimental branch that I am suspending in the middle of a work in progress to make mrb_obj_alloc() more rigorous.
master...dearblue:mruby:wip/alloc-strict
The reason I am suspending it is because of compatibility concerns and at least my mruby-lz4 needs to be modified.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will merge this PR as it is. Then I will try to update mrb_obj_allocate.

@matz matz merged commit 82f6fac into mruby:master Nov 7, 2023
@dearblue dearblue deleted the fiber-new branch December 1, 2023 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants