Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
b97669ab
Commit
b97669ab
authored
Jan 8, 2014
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Improve __build_class__.
parent
6d6bc9ef
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
py/builtin.c
+42
-10
42 additions, 10 deletions
py/builtin.c
py/builtin.h
+1
-1
1 addition, 1 deletion
py/builtin.h
py/obj.c
+9
-0
9 additions, 0 deletions
py/obj.c
py/obj.h
+1
-0
1 addition, 0 deletions
py/obj.h
py/runtime.c
+1
-1
1 addition, 1 deletion
py/runtime.c
with
54 additions
and
12 deletions
py/builtin.c
+
42
−
10
View file @
b97669ab
...
...
@@ -15,22 +15,59 @@
#include
"map.h"
#include
"builtin.h"
mp_obj_t
mp_builtin___build_class__
(
mp_obj_t
o_class_fun
,
mp_obj_t
o_class_name
)
{
// args[0] is function from class body
// args[1] is class name
// args[2:] are base objects
mp_obj_t
mp_builtin___build_class__
(
int
n_args
,
const
mp_obj_t
*
args
)
{
assert
(
2
<=
n_args
);
// we differ from CPython: we set the new __locals__ object here
mp_map_t
*
old_locals
=
rt_locals_get
();
mp_map_t
*
class_locals
=
mp_map_new
(
MP_MAP_QSTR
,
0
);
rt_locals_set
(
class_locals
);
// call the class code
rt_call_function_1
(
o_class_fun
,
(
mp_obj_t
)
0xdeadbeef
);
mp_obj_t
cell
=
rt_call_function_1
(
args
[
0
]
,
(
mp_obj_t
)
0xdeadbeef
);
// restore old __locals__ object
rt_locals_set
(
old_locals
);
// create and return the new class
return
mp_obj_new_class
(
class_locals
);
/*
// get the class type (meta object) from the base objects
mp_obj_t meta;
if (n_args == 2) {
// no explicit bases, so use 'type'
meta = (mp_obj_t)&mp_const_type;
} else {
// use type of first base object
meta = mp_obj_get_type(args[2]);
}
*/
// TODO do proper metaclass resolution for multiple base objects
/*
// create the new class using a call to the meta object
// (arguments must be backwards in the array)
mp_obj_t meta_args[3];
meta_args[2] = args[1]; // class name
meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases
meta_args[0] = class_locals; // dict of members TODO, currently is a map
mp_obj_t new_class = rt_call_function_n(meta, 3, meta_args);
*/
// create the new class
mp_obj_t
new_class
=
mp_obj_new_class
(
class_locals
);
// store into cell if neede
if
(
cell
!=
mp_const_none
)
{
mp_obj_cell_set
(
cell
,
new_class
);
}
return
new_class
;
}
MP_DEFINE_CONST_FUN_OBJ_VAR
(
mp_builtin___build_class___obj
,
2
,
mp_builtin___build_class__
);
mp_obj_t
mp_builtin___repl_print__
(
mp_obj_t
o
)
{
if
(
o
!=
mp_const_none
)
{
mp_obj_print
(
o
);
...
...
@@ -281,12 +318,7 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
static
mp_obj_t
mp_builtin_type
(
mp_obj_t
o_in
)
{
// TODO implement the 3 argument version of type()
if
(
MP_OBJ_IS_SMALL_INT
(
o_in
))
{
return
(
mp_obj_t
)
&
int_type
;
}
else
{
mp_obj_base_t
*
o
=
o_in
;
return
(
mp_obj_t
)
o
->
type
;
}
return
mp_obj_get_type
(
o_in
);
}
MP_DEFINE_CONST_FUN_OBJ_1
(
mp_builtin_type_obj
,
mp_builtin_type
);
This diff is collapsed.
Click to expand it.
py/builtin.h
+
1
−
1
View file @
b97669ab
// TODO convert all these to objects using MP_DECLARE and MP_DEFINE
mp_obj_t
mp_builtin___build_class__
(
mp
_obj
_t
o_class_fun
,
mp_obj_t
o_class_name
);
MP_DECLARE_CONST_FUN_OBJ
(
mp_builtin___build_class___obj
);
mp_obj_t
mp_builtin___import__
(
int
n
,
mp_obj_t
*
args
);
mp_obj_t
mp_builtin___repl_print__
(
mp_obj_t
o
);
mp_obj_t
mp_builtin_abs
(
mp_obj_t
o_in
);
...
...
This diff is collapsed.
Click to expand it.
py/obj.c
+
9
−
0
View file @
b97669ab
...
...
@@ -13,6 +13,15 @@
#include
"runtime.h"
#include
"map.h"
mp_obj_t
mp_obj_get_type
(
mp_obj_t
o_in
)
{
if
(
MP_OBJ_IS_SMALL_INT
(
o_in
))
{
return
(
mp_obj_t
)
&
int_type
;
}
else
{
mp_obj_base_t
*
o
=
o_in
;
return
(
mp_obj_t
)
o
->
type
;
}
}
const
char
*
mp_obj_get_type_str
(
mp_obj_t
o_in
)
{
if
(
MP_OBJ_IS_SMALL_INT
(
o_in
))
{
return
"int"
;
...
...
This diff is collapsed.
Click to expand it.
py/obj.h
+
1
−
0
View file @
b97669ab
...
...
@@ -161,6 +161,7 @@ mp_obj_t mp_obj_new_class(struct _mp_map_t *class_locals);
mp_obj_t
mp_obj_new_instance
(
mp_obj_t
clas
);
mp_obj_t
mp_obj_new_module
(
qstr
module_name
);
mp_obj_t
mp_obj_get_type
(
mp_obj_t
o_in
);
const
char
*
mp_obj_get_type_str
(
mp_obj_t
o_in
);
void
mp_obj_print_helper
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
o_in
);
...
...
This diff is collapsed.
Click to expand it.
py/runtime.c
+
1
−
1
View file @
b97669ab
...
...
@@ -90,7 +90,7 @@ void rt_init(void) {
mp_qstr_map_lookup
(
&
map_builtins
,
MP_QSTR_Ellipsis
,
true
)
->
value
=
mp_const_ellipsis
;
// built-in core functions
mp_qstr_map_lookup
(
&
map_builtins
,
MP_QSTR___build_class__
,
true
)
->
value
=
rt_make_function_2
(
mp_builtin___build_class__
)
;
mp_qstr_map_lookup
(
&
map_builtins
,
MP_QSTR___build_class__
,
true
)
->
value
=
(
mp_obj_t
)
&
mp_builtin___build_class__
_obj
;
mp_qstr_map_lookup
(
&
map_builtins
,
MP_QSTR___repl_print__
,
true
)
->
value
=
rt_make_function_1
(
mp_builtin___repl_print__
);
// built-in types
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment