Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
f0e25121
Unverified
Commit
f0e25121
authored
Feb 10, 2021
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'mruby-catch' of
https://github.com/dearblue/mruby
into dearblue-mruby-catch
parents
20342714
232e07ad
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
8 deletions
+78
-8
mrbgems/mruby-catch/mrblib/catch.rb
mrbgems/mruby-catch/mrblib/catch.rb
+8
-8
mrbgems/mruby-catch/src/mruby-catch.c
mrbgems/mruby-catch/src/mruby-catch.c
+70
-0
No files found.
mrbgems/mruby-catch/mrblib/catch.rb
View file @
f0e25121
class
ThrowCatchJump
<
Exception
class
UncaughtThrowError
<
ArgumentError
attr_reader
:_tag
,
:_val
def
initialize
(
tag
,
val
)
@_tag
=
tag
...
...
@@ -9,14 +9,14 @@ end
module
Kernel
def
catch
(
tag
=
Object
.
new
,
&
block
)
block
.
call
(
tag
)
rescue
ThrowCatchJump
=>
e
unless
e
.
_tag
.
equal?
(
tag
)
raise
e
end
return
e
.
_val
# A double closure is required to make the nested `catch` distinguishable
# and because `break` goes back to `proc->upper`.
->
{
->
{
block
.
call
(
tag
)
}.
call
}.
call
end
def
throw
(
tag
,
val
=
nil
)
raise
ThrowCatchJump
.
new
(
tag
,
val
)
__throw
(
tag
,
val
)
raise
UncaughtThrowError
.
new
(
tag
,
val
)
end
__preserve_catch_method
end
mrbgems/mruby-catch/src/mruby-catch.c
0 → 100644
View file @
f0e25121
#include <mruby.h>
#include <mruby/class.h>
#include <mruby/variable.h>
#include <mruby/error.h>
#include <mruby/proc.h>
#include <mruby/presym.h>
#define ID_PRESERVED_CATCH MRB_SYM(__preserved_catch_proc)
static
const
mrb_callinfo
*
find_catcher
(
mrb_state
*
mrb
,
mrb_value
tag
)
{
mrb_value
pval
=
mrb_obj_iv_get
(
mrb
,
(
struct
RObject
*
)
mrb
->
kernel_module
,
ID_PRESERVED_CATCH
);
mrb_assert
(
mrb_proc_p
(
pval
));
const
struct
RProc
*
proc
=
mrb_proc_ptr
(
pval
);
const
mrb_callinfo
*
ci
=
mrb
->
c
->
ci
;
size_t
n
=
ci
-
mrb
->
c
->
cibase
;
ci
--
;
for
(;
n
>
0
;
n
--
,
ci
--
)
{
const
mrb_value
*
arg1
=
ci
->
stack
+
1
;
if
(
ci
->
proc
==
proc
&&
mrb_obj_eq
(
mrb
,
*
arg1
,
tag
))
{
return
ci
;
}
}
return
NULL
;
}
static
mrb_value
mrb_f_throw
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_value
tag
,
obj
;
mrb_get_args
(
mrb
,
"oo"
,
&
tag
,
&
obj
);
const
mrb_callinfo
*
ci
=
find_catcher
(
mrb
,
tag
);
if
(
ci
)
{
struct
RBreak
*
b
=
(
struct
RBreak
*
)
mrb_obj_alloc
(
mrb
,
MRB_TT_BREAK
,
NULL
);
mrb_break_value_set
(
b
,
obj
);
mrb_break_proc_set
(
b
,
ci
[
2
].
proc
);
/* Back to the closure in `catch` method */
mrb_exc_raise
(
mrb
,
mrb_obj_value
(
b
));
}
return
mrb_nil_value
();
}
static
mrb_value
mrb_s_preserve_catch
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_method_t
m
=
mrb_method_search
(
mrb
,
mrb
->
kernel_module
,
MRB_SYM
(
catch
));
mrb_assert
(
!
MRB_METHOD_UNDEF_P
(
m
));
mrb_assert
(
!
MRB_METHOD_CFUNC_P
(
m
));
mrb_obj_iv_set
(
mrb
,
(
struct
RObject
*
)
mrb
->
kernel_module
,
ID_PRESERVED_CATCH
,
mrb_obj_value
(
MRB_METHOD_PROC
(
m
)));
mrb_remove_method
(
mrb
,
mrb_class
(
mrb
,
mrb_obj_value
(
mrb
->
kernel_module
)),
MRB_SYM
(
__preserve_catch_method
));
return
mrb_nil_value
();
}
void
mrb_mruby_catch_gem_init
(
mrb_state
*
mrb
)
{
mrb_define_method
(
mrb
,
mrb
->
kernel_module
,
"__throw"
,
mrb_f_throw
,
MRB_ARGS_REQ
(
2
));
mrb_define_class_method
(
mrb
,
mrb
->
kernel_module
,
"__preserve_catch_method"
,
mrb_s_preserve_catch
,
MRB_ARGS_NONE
());
}
void
mrb_mruby_catch_gem_final
(
mrb_state
*
mrb
)
{
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment