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
2a92fb25
Unverified
Commit
2a92fb25
authored
Aug 18, 2020
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make division by zero cause `ZeroDivisionError`.
As described in ISO 15.2.30.
parent
18e3d39e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
24 additions
and
25 deletions
+24
-25
doc/limitations.md
doc/limitations.md
+8
-1
include/mruby.h
include/mruby.h
+1
-0
mrblib/10error.rb
mrblib/10error.rb
+4
-0
src/numeric.c
src/numeric.c
+3
-17
src/vm.c
src/vm.c
+7
-6
test/t/superclass.rb
test/t/superclass.rb
+1
-1
No files found.
doc/limitations.md
View file @
2a92fb25
...
...
@@ -63,8 +63,15 @@ end
#### mruby [2.1.2 (2020-08-06)]
No exception is raised.
No exception is raised.
Instead you have to do:
```
ruby
begin
1
/
0
rescue
=>
e
raise
e
end
```
## Fiber execution can't cross C function boundary
mruby's
`Fiber`
is implemented in a similar way to Lua's co-routine. This
...
...
include/mruby.h
View file @
2a92fb25
...
...
@@ -1292,6 +1292,7 @@ MRB_API mrb_value mrb_vformat(mrb_state *mrb, const char *format, va_list ap);
*/
#define E_RUNTIME_ERROR (mrb_exc_get_id(mrb, MRB_SYM(RuntimeError)))
#define E_TYPE_ERROR (mrb_exc_get_id(mrb, MRB_SYM(TypeError)))
#define E_ZERODIV_ERROR (mrb_exc_get_id(mrb, MRB_SYM(ZeroDivisionError)))
#define E_ARGUMENT_ERROR (mrb_exc_get_id(mrb, MRB_SYM(ArgumentError)))
#define E_INDEX_ERROR (mrb_exc_get_id(mrb, MRB_SYM(IndexError)))
#define E_RANGE_ERROR (mrb_exc_get_id(mrb, MRB_SYM(RangeError)))
...
...
mrblib/10error.rb
View file @
2a92fb25
...
...
@@ -21,6 +21,10 @@ end
class
TypeError
<
StandardError
end
# ISO 15.2.30
class
ZeroDivisionError
<
StandardError
end
# ISO 15.2.31
class
NameError
<
StandardError
attr_accessor
:name
...
...
src/numeric.c
View file @
2a92fb25
...
...
@@ -156,7 +156,7 @@ integral_div(mrb_state *mrb, mrb_value xv)
mrb_get_args
(
mrb
,
"i"
,
&
y
);
if
(
y
==
0
)
{
mrb_raise
(
mrb
,
E_
RUNTIME
_ERROR
,
"devided by zero"
);
mrb_raise
(
mrb
,
E_
ZERODIV
_ERROR
,
"devided by zero"
);
}
return
mrb_fixnum_value
(
mrb_fixnum
(
xv
)
/
y
);
#else
...
...
@@ -932,14 +932,7 @@ int_mod(mrb_state *mrb, mrb_value x)
mrb_int
mod
;
if
(
b
==
0
)
{
#ifdef MRB_NO_FLOAT
/* ZeroDivisionError */
return
mrb_fixnum_value
(
0
);
#else
if
(
a
>
0
)
return
mrb_float_value
(
mrb
,
INFINITY
);
if
(
a
<
0
)
return
mrb_float_value
(
mrb
,
INFINITY
);
return
mrb_float_value
(
mrb
,
NAN
);
#endif
mrb_raise
(
mrb
,
E_ZERODIV_ERROR
,
"divided by 0"
);
}
fixdivmod
(
mrb
,
a
,
b
,
NULL
,
&
mod
);
return
mrb_fixnum_value
(
mod
);
...
...
@@ -971,14 +964,7 @@ int_divmod(mrb_state *mrb, mrb_value x)
mrb_int
div
,
mod
;
if
(
mrb_fixnum
(
y
)
==
0
)
{
#ifdef MRB_NO_FLOAT
return
mrb_assoc_new
(
mrb
,
mrb_fixnum_value
(
0
),
mrb_fixnum_value
(
0
));
#else
return
mrb_assoc_new
(
mrb
,
((
mrb_fixnum
(
x
)
==
0
)
?
mrb_float_value
(
mrb
,
NAN
)
:
mrb_float_value
(
mrb
,
INFINITY
)),
mrb_float_value
(
mrb
,
NAN
));
#endif
mrb_raise
(
mrb
,
E_ZERODIV_ERROR
,
"divided by 0"
);
}
fixdivmod
(
mrb
,
mrb_fixnum
(
x
),
mrb_fixnum
(
y
),
&
div
,
&
mod
);
return
mrb_assoc_new
(
mrb
,
mrb_fixnum_value
(
div
),
mrb_fixnum_value
(
mod
));
...
...
src/vm.c
View file @
2a92fb25
...
...
@@ -2315,12 +2315,13 @@ RETRY_TRY_BLOCK:
{
mrb_int
x
=
mrb_fixnum
(
regs
[
a
]);
mrb_int
y
=
mrb_fixnum
(
regs
[
a
+
1
]);
if
(
y
==
0
||
(
x
==
MRB_INT_MIN
&&
y
==
-
1
))
{
#ifdef MRB_NO_FLOAT
SET_INT_VALUE
(
regs
[
a
],
y
?
x
/
y
:
0
);
#else
SET_FLOAT_VALUE
(
mrb
,
regs
[
a
],
(
mrb_float
)
x
/
(
mrb_float
)
y
);
#endif
if
(
y
==
0
)
{
mrb_raise
(
mrb
,
E_ZERODIV_ERROR
,
"divided by 0"
);
}
else
if
(
x
==
MRB_INT_MIN
&&
y
==
-
1
)
{
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
"integer overflow in division"
);
}
else
{
mrb_int
div
,
mod
;
...
...
test/t/superclass.rb
View file @
2a92fb25
...
...
@@ -29,7 +29,7 @@
[
:RegexpError
,
:StandardError
,
'12.2.27.2'
],
[
:RuntimeError
,
:StandardError
,
'12.2.28.2'
],
[
:TypeError
,
:StandardError
,
'12.2.29.2'
],
# [:ZeroDivisionError, :StandardError, '12.2.30.2'], # No ZeroDivisionError in mruby
[
:ZeroDivisionError
,
:StandardError
,
'12.2.30.2'
],
[
:NameError
,
:StandardError
,
'15.2.31.2'
],
[
:NoMethodError
,
:NameError
,
'15.2.32.2'
],
[
:IndexError
,
:StandardError
,
'15.2.33.2'
],
...
...
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