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
a1e12011
Unverified
Commit
a1e12011
authored
Mar 15, 2019
by
Yukihiro "Matz" Matsumoto
Committed by
GitHub
Mar 15, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4328 from shuujii/fix-constant-name-validation
Fix constant name validation
parents
298807fa
16b1b297
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
25 additions
and
22 deletions
+25
-22
include/mruby/variable.h
include/mruby/variable.h
+1
-0
src/class.c
src/class.c
+1
-1
src/variable.c
src/variable.c
+13
-7
test/t/module.rb
test/t/module.rb
+10
-14
No files found.
include/mruby/variable.h
View file @
a1e12011
...
...
@@ -123,6 +123,7 @@ mrb_value mrb_obj_instance_variables(mrb_state*, mrb_value);
mrb_value
mrb_mod_class_variables
(
mrb_state
*
,
mrb_value
);
mrb_value
mrb_mod_cv_get
(
mrb_state
*
mrb
,
struct
RClass
*
c
,
mrb_sym
sym
);
mrb_bool
mrb_mod_cv_defined
(
mrb_state
*
mrb
,
struct
RClass
*
c
,
mrb_sym
sym
);
mrb_bool
mrb_ident_p
(
const
char
*
s
,
mrb_int
len
);
/* GC functions */
void
mrb_gc_mark_gv
(
mrb_state
*
);
...
...
src/class.c
View file @
a1e12011
...
...
@@ -1898,7 +1898,7 @@ mrb_mod_undef(mrb_state *mrb, mrb_value mod)
static
mrb_bool
const_name_p
(
mrb_state
*
mrb
,
const
char
*
name
,
mrb_int
len
)
{
return
len
>
0
&&
ISUPPER
(
name
[
0
]);
return
len
>
0
&&
ISUPPER
(
name
[
0
])
&&
mrb_ident_p
(
name
+
1
,
len
-
1
)
;
}
static
void
...
...
src/variable.c
View file @
a1e12011
...
...
@@ -428,22 +428,17 @@ mrb_iv_defined(mrb_state *mrb, mrb_value obj, mrb_sym sym)
return
mrb_obj_iv_defined
(
mrb
,
mrb_obj_ptr
(
obj
),
sym
);
}
#define identchar(c) (ISALNUM(c) || (c) == '_' || !ISASCII(c))
MRB_API
mrb_bool
mrb_iv_name_sym_p
(
mrb_state
*
mrb
,
mrb_sym
iv_name
)
{
const
char
*
s
;
mrb_int
i
,
len
;
mrb_int
len
;
s
=
mrb_sym2name_len
(
mrb
,
iv_name
,
&
len
);
if
(
len
<
2
)
return
FALSE
;
if
(
s
[
0
]
!=
'@'
)
return
FALSE
;
if
(
s
[
1
]
==
'@'
)
return
FALSE
;
for
(
i
=
1
;
i
<
len
;
i
++
)
{
if
(
!
identchar
(
s
[
i
]))
return
FALSE
;
}
return
TRUE
;
return
mrb_ident_p
(
s
+
1
,
len
-
1
);
}
MRB_API
void
...
...
@@ -1114,3 +1109,14 @@ mrb_class_find_path(mrb_state *mrb, struct RClass *c)
}
return
path
;
}
#define identchar(c) (ISALNUM(c) || (c) == '_' || !ISASCII(c))
mrb_bool
mrb_ident_p
(
const
char
*
s
,
mrb_int
len
)
{
for
(
mrb_int
i
=
0
;
i
<
len
;
i
++
)
{
if
(
!
identchar
(
s
[
i
]))
return
FALSE
;
}
return
TRUE
;
}
test/t/module.rb
View file @
a1e12011
...
...
@@ -210,6 +210,7 @@ assert('Module#const_defined?', '15.2.2.4.20') do
assert_true
Test4ConstDefined
.
const_defined?
(
:Const4Test4ConstDefined
)
assert_false
Test4ConstDefined
.
const_defined?
(
:NotExisting
)
assert_raise
(
NameError
){
Test4ConstDefined
.
const_defined?
(
:wrong_name
)
}
end
assert
(
'Module#const_get'
,
'15.2.2.4.21'
)
do
...
...
@@ -224,6 +225,7 @@ assert('Module#const_get', '15.2.2.4.21') do
assert_raise
(
TypeError
){
Test4ConstGet
.
const_get
(
123
)
}
assert_raise
(
NameError
){
Test4ConstGet
.
const_get
(
:I_DO_NOT_EXIST
)
}
assert_raise
(
NameError
){
Test4ConstGet
.
const_get
(
"I_DO_NOT_EXIST::ME_NEITHER"
)
}
assert_raise
(
NameError
){
Test4ConstGet
.
const_get
(
:wrong_name
)
}
end
assert
(
'Module#const_set'
,
'15.2.2.4.23'
)
do
...
...
@@ -233,6 +235,9 @@ assert('Module#const_set', '15.2.2.4.23') do
assert_equal
23
,
Test4ConstSet
.
const_set
(
:Const4Test4ConstSet
,
23
)
assert_equal
23
,
Test4ConstSet
.
const_get
(
:Const4Test4ConstSet
)
[
""
,
"wrongNAME"
,
"Wrong-Name"
].
each
do
|
n
|
assert_raise
(
NameError
)
{
Test4ConstSet
.
const_set
(
n
,
1
)
}
end
end
assert
(
'Module#remove_const'
,
'15.2.2.4.40'
)
do
...
...
@@ -240,21 +245,12 @@ assert('Module#remove_const', '15.2.2.4.40') do
ExistingConst
=
23
end
result
=
Test4RemoveConst
.
module_eval
{
remove_const
:ExistingConst
}
name_error
=
false
begin
Test4RemoveConst
.
module_eval
{
remove_const
:NonExistingConst
}
rescue
NameError
name_error
=
true
assert_equal
23
,
Test4RemoveConst
.
remove_const
(
:ExistingConst
)
assert_false
Test4RemoveConst
.
const_defined?
(
:ExistingConst
)
assert_raise
(
NameError
)
{
Test4RemoveConst
.
remove_const
(
:NonExistingConst
)
}
%i[x X!]
.
each
do
|
n
|
assert_raise
(
NameError
)
{
Test4RemoveConst
.
remove_const
(
n
)
}
end
# Constant removed from Module
assert_false
Test4RemoveConst
.
const_defined?
:ExistingConst
# Return value of binding
assert_equal
23
,
result
# Name Error raised when Constant doesn't exist
assert_true
name_error
end
assert
(
'Module#const_missing'
,
'15.2.2.4.22'
)
do
...
...
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