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
42935981
Commit
42935981
authored
Dec 21, 2012
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #662 from skandhas/pr-add-Module-class_variable_set
Add Module#class_variable_set for mruby
parents
46a3bd76
f612f32a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
0 deletions
+80
-0
include/mruby/variable.h
include/mruby/variable.h
+2
-0
src/class.c
src/class.c
+33
-0
src/variable.c
src/variable.c
+30
-0
test/t/module.rb
test/t/module.rb
+15
-0
No files found.
include/mruby/variable.h
View file @
42935981
...
...
@@ -57,6 +57,8 @@ mrb_sym mrb_class_sym(mrb_state *mrb, struct RClass *c, struct RClass *outer);
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_value
mrb_cv_get
(
mrb_state
*
mrb
,
mrb_value
mod
,
mrb_sym
sym
);
void
mrb_mod_cv_set
(
mrb_state
*
mrb
,
struct
RClass
*
c
,
mrb_sym
sym
,
mrb_value
v
);
void
mrb_cv_set
(
mrb_state
*
mrb
,
mrb_value
mod
,
mrb_sym
sym
,
mrb_value
v
);
/* GC functions */
void
mrb_gc_mark_gv
(
mrb_state
*
);
...
...
src/class.c
View file @
42935981
...
...
@@ -1483,6 +1483,38 @@ mrb_mod_cvar_get(mrb_state *mrb, mrb_value mod)
return
mrb_cv_get
(
mrb
,
mod
,
id
);
}
/* 15.2.2.4.18 */
/*
* call-seq:
* obj.class_variable_set(symbol, obj) -> obj
*
* Sets the class variable names by <i>symbol</i> to
* <i>object</i>.
*
* class Fred
* @@foo = 99
* def foo
* @@foo
* end
* end
* Fred.class_variable_set(:@@foo, 101) #=> 101
* Fred.new.foo #=> 101
*/
static
mrb_value
mrb_mod_cvar_set
(
mrb_state
*
mrb
,
mrb_value
mod
)
{
mrb_value
sym
,
value
;
mrb_sym
id
;
mrb_get_args
(
mrb
,
"oo"
,
&
sym
,
&
value
);
id
=
mrb_sym_value
(
mrb
,
sym
);
check_cv_name
(
mrb
,
id
);
mrb_cv_set
(
mrb
,
mod
,
id
,
value
);
return
value
;
}
static
void
check_const_name
(
mrb_state
*
mrb
,
mrb_sym
id
)
{
...
...
@@ -1585,6 +1617,7 @@ mrb_init_class(mrb_state *mrb)
mrb_define_method
(
mrb
,
cls
,
"new"
,
mrb_instance_new
,
ARGS_ANY
());
/* 15.2.3.3.3 */
mrb_define_method
(
mrb
,
cls
,
"inherited"
,
mrb_bob_init
,
ARGS_REQ
(
1
));
mrb_define_method
(
mrb
,
mod
,
"class_variable_get"
,
mrb_mod_cvar_get
,
ARGS_REQ
(
1
));
/* 15.2.2.4.17 */
mrb_define_method
(
mrb
,
mod
,
"class_variable_set"
,
mrb_mod_cvar_set
,
ARGS_REQ
(
2
));
/* 15.2.2.4.18 */
mrb_define_method
(
mrb
,
mod
,
"extend_object"
,
mrb_mod_extend_object
,
ARGS_REQ
(
1
));
/* 15.2.2.4.25 */
mrb_define_method
(
mrb
,
mod
,
"extended"
,
mrb_bob_init
,
ARGS_REQ
(
1
));
/* 15.2.2.4.26 */
mrb_define_method
(
mrb
,
mod
,
"include"
,
mrb_mod_include
,
ARGS_ANY
());
/* 15.2.2.4.27 */
...
...
src/variable.c
View file @
42935981
...
...
@@ -695,6 +695,36 @@ mrb_cv_get(mrb_state *mrb, mrb_value mod, mrb_sym sym)
return
mrb_mod_cv_get
(
mrb
,
mrb_class_ptr
(
mod
),
sym
);
}
void
mrb_mod_cv_set
(
mrb_state
*
mrb
,
struct
RClass
*
c
,
mrb_sym
sym
,
mrb_value
v
)
{
struct
RClass
*
cls
=
c
;
while
(
c
)
{
if
(
c
->
iv
)
{
iv_tbl
*
t
=
c
->
iv
;
if
(
iv_get
(
mrb
,
t
,
sym
,
NULL
))
{
iv_put
(
mrb
,
t
,
sym
,
v
);
return
;
}
}
c
=
c
->
super
;
}
if
(
!
cls
->
iv
)
{
cls
->
iv
=
iv_new
(
mrb
);
}
iv_put
(
mrb
,
cls
->
iv
,
sym
,
v
);
}
void
mrb_cv_set
(
mrb_state
*
mrb
,
mrb_value
mod
,
mrb_sym
sym
,
mrb_value
v
)
{
mrb_mod_cv_set
(
mrb
,
mrb_class_ptr
(
mod
),
sym
,
v
);
}
mrb_value
mrb_vm_cv_get
(
mrb_state
*
mrb
,
mrb_sym
sym
)
{
...
...
test/t/module.rb
View file @
42935981
...
...
@@ -54,6 +54,21 @@ assert('Module#class_variable_get', '15.2.2.4.17') do
Test4ClassVariableGet
.
class_variable_get
(
:@@cv
)
==
99
end
assert
(
'Module#class_variable_set'
,
'15.2.2.4.18'
)
do
class
Test4ClassVariableSet
@@foo
=
100
def
foo
@@foo
end
end
Test4ClassVariableSet
.
class_variable_set
(
:@@cv
,
99
)
Test4ClassVariableSet
.
class_variable_set
(
:@@foo
,
101
)
Test4ClassVariableSet
.
class_variables
.
include?
:@@cv
and
Test4ClassVariableSet
.
class_variable_get
(
:@@cv
)
==
99
and
Test4ClassVariableSet
.
new
.
foo
==
101
end
assert
(
'Module#class_variables'
,
'15.2.2.4.19'
)
do
class
Test4ClassVariables1
...
...
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