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
2c3530ff
Commit
2c3530ff
authored
Mar 31, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1985 from take-cheeze/cfunc_with_env
Add API to define cfunc Proc with userdata.
parents
d1e755df
bf6b1dfe
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
126 additions
and
0 deletions
+126
-0
include/mruby/proc.h
include/mruby/proc.h
+4
-0
mrbgems/mruby-proc-ext/src/proc.c
mrbgems/mruby-proc-ext/src/proc.c
+43
-0
mrbgems/mruby-proc-ext/test/proc.c
mrbgems/mruby-proc-ext/test/proc.c
+56
-0
mrbgems/mruby-proc-ext/test/proc.rb
mrbgems/mruby-proc-ext/test/proc.rb
+23
-0
No files found.
include/mruby/proc.h
View file @
2c3530ff
...
...
@@ -55,6 +55,10 @@ void mrb_proc_copy(struct RProc *a, struct RProc *b);
/* implementation of #send method */
mrb_value
mrb_f_send
(
mrb_state
*
mrb
,
mrb_value
self
);
/* following functions are defined in mruby-proc-ext so please include it when using */
struct
RProc
*
mrb_proc_new_cfunc_with_env
(
mrb_state
*
,
mrb_func_t
,
mrb_int
,
const
mrb_value
*
);
mrb_value
mrb_cfunc_env_get
(
mrb_state
*
,
mrb_int
);
#include "mruby/khash.h"
KHASH_DECLARE
(
mt
,
mrb_sym
,
struct
RProc
*
,
1
)
...
...
mrbgems/mruby-proc-ext/src/proc.c
View file @
2c3530ff
...
...
@@ -4,6 +4,49 @@
#include "mruby/string.h"
#include "mruby/debug.h"
struct
RProc
*
mrb_proc_new_cfunc_with_env
(
mrb_state
*
mrb
,
mrb_func_t
f
,
mrb_int
argc
,
const
mrb_value
*
argv
)
{
struct
RProc
*
p
;
struct
REnv
*
e
;
int
ai
,
i
;
p
=
mrb_proc_new_cfunc
(
mrb
,
f
);
ai
=
mrb_gc_arena_save
(
mrb
);
e
=
(
struct
REnv
*
)
mrb_obj_alloc
(
mrb
,
MRB_TT_ENV
,
NULL
);
p
->
env
=
e
;
mrb_gc_arena_restore
(
mrb
,
ai
);
e
->
cioff
=
-
1
;
e
->
flags
=
argc
;
e
->
stack
=
(
mrb_value
*
)
mrb_malloc
(
mrb
,
sizeof
(
mrb_value
)
*
argc
);
for
(
i
=
0
;
i
<
argc
;
++
i
)
{
e
->
stack
[
i
]
=
argv
[
i
];
}
return
p
;
}
mrb_value
mrb_cfunc_env_get
(
mrb_state
*
mrb
,
mrb_int
idx
)
{
struct
RProc
*
p
=
mrb
->
c
->
ci
->
proc
;
struct
REnv
*
e
=
p
->
env
;
if
(
!
MRB_PROC_CFUNC_P
(
p
))
{
mrb_raise
(
mrb
,
E_TYPE_ERROR
,
"Can't get cfunc env from non-cfunc proc."
);
}
if
(
!
e
)
{
mrb_raise
(
mrb
,
E_TYPE_ERROR
,
"Can't get cfunc env from cfunc Proc without REnv."
);
}
if
(
idx
<
0
||
e
->
flags
<=
idx
)
{
mrb_raisef
(
mrb
,
E_INDEX_ERROR
,
"Env index out of range: %S (expected: 0 <= index < %S)"
,
mrb_fixnum_value
(
idx
),
mrb_fixnum_value
(
e
->
flags
));
}
return
e
->
stack
[
idx
];
}
static
mrb_value
mrb_proc_lambda
(
mrb_state
*
mrb
,
mrb_value
self
)
{
...
...
mrbgems/mruby-proc-ext/test/proc.c
0 → 100644
View file @
2c3530ff
#include "mruby.h"
#include "mruby/proc.h"
#include "mruby/class.h"
static
mrb_value
return_func_name
(
mrb_state
*
mrb
,
mrb_value
self
)
{
return
mrb_cfunc_env_get
(
mrb
,
0
);
}
static
mrb_value
proc_new_cfunc_with_env
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_sym
n
;
mrb_value
n_val
;
mrb_get_args
(
mrb
,
"n"
,
&
n
);
n_val
=
mrb_symbol_value
(
n
);
mrb_define_method_raw
(
mrb
,
mrb_class_ptr
(
self
),
n
,
mrb_proc_new_cfunc_with_env
(
mrb
,
return_func_name
,
1
,
&
n_val
));
return
self
;
}
static
mrb_value
return_env
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_int
idx
;
mrb_get_args
(
mrb
,
"i"
,
&
idx
);
return
mrb_cfunc_env_get
(
mrb
,
idx
);
}
static
mrb_value
cfunc_env_get
(
mrb_state
*
mrb
,
mrb_value
self
)
{
mrb_sym
n
;
mrb_value
*
argv
;
mrb_int
argc
;
mrb_get_args
(
mrb
,
"na"
,
&
n
,
&
argv
,
&
argc
);
mrb_define_method_raw
(
mrb
,
mrb_class_ptr
(
self
),
n
,
mrb_proc_new_cfunc_with_env
(
mrb
,
return_env
,
argc
,
argv
));
return
self
;
}
static
mrb_value
cfunc_without_env
(
mrb_state
*
mrb
,
mrb_value
self
)
{
return
mrb_cfunc_env_get
(
mrb
,
0
);
}
void
mrb_mruby_proc_ext_gem_test
(
mrb_state
*
mrb
)
{
struct
RClass
*
cls
;
cls
=
mrb_define_class
(
mrb
,
"ProcExtTest"
,
mrb
->
object_class
);
mrb_define_module_function
(
mrb
,
cls
,
"mrb_proc_new_cfunc_with_env"
,
proc_new_cfunc_with_env
,
MRB_ARGS_REQ
(
1
));
mrb_define_module_function
(
mrb
,
cls
,
"mrb_cfunc_env_get"
,
cfunc_env_get
,
MRB_ARGS_REQ
(
2
));
mrb_define_module_function
(
mrb
,
cls
,
"cfunc_without_env"
,
cfunc_without_env
,
MRB_ARGS_NONE
());
}
mrbgems/mruby-proc-ext/test/proc.rb
View file @
2c3530ff
...
...
@@ -46,3 +46,26 @@ end
assert
(
'Kernel#proc'
)
do
assert_true
!
proc
{
|
a
|
}.
lambda?
end
assert
(
'mrb_proc_new_cfunc_with_env'
)
do
ProcExtTest
.
mrb_proc_new_cfunc_with_env
(
:test
)
ProcExtTest
.
mrb_proc_new_cfunc_with_env
(
:mruby
)
t
=
ProcExtTest
.
new
assert_equal
:test
,
t
.
test
assert_equal
:mruby
,
t
.
mruby
end
assert
(
'mrb_cfunc_env_get'
)
do
ProcExtTest
.
mrb_cfunc_env_get
:get_int
,
[
0
,
1
,
2
]
t
=
ProcExtTest
.
new
assert_raise
(
TypeError
)
{
t
.
cfunc_without_env
}
assert_raise
(
IndexError
)
{
t
.
get_int
(
-
1
)
}
assert_raise
(
IndexError
)
{
t
.
get_int
(
3
)
}
assert_equal
1
,
t
.
get_int
(
1
)
end
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