Skip to content

Commit

Permalink
Berry internal: remove class from closure to simplify code (#21839)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger authored Jul 24, 2024
1 parent db3e29d commit bf7fbf2
Show file tree
Hide file tree
Showing 99 changed files with 1,585 additions and 3,007 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ All notable changes to this project will be documented in this file.
- Berry fix `bytes` setters and getters with negative offsets

### Removed

- Berry internal: remove class from closure to simplify code

## [14.1.0.3] 20240722
### Added
Expand Down
16 changes: 0 additions & 16 deletions lib/libesp32/berry/src/be_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,22 +544,6 @@ BERRY_API bbool be_classof(bvm *vm, int index)
binstance *ins = var_toobj(v);
var_setclass(top, be_instance_class(ins));
return btrue;
} else if (var_isclosure(v)) {
bclosure *cl = var_toobj(v);
bproto *pr = cl->proto;
if (pr != NULL) {
bclass *cla;
if (pr->nproto > 0) {
cla = (bclass*) pr->ptab[pr->nproto];
} else {
cla = (bclass*) pr->ptab;
}
if (cla && var_basetype(cla) == BE_CLASS) {
bvalue *top = be_incrtop(vm);
var_setclass(top, cla);
return btrue;
}
}
}
return bfalse;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/libesp32/berry/src/be_bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ static void load_proto_table(bvm *vm, void *fp, bproto *proto, int info, int ver
{
int size = (int)load_long(fp); /* proto count */
if (size) {
bproto **p = be_malloc(vm, sizeof(bproto *) * (size + 1));
memset(p, 0, sizeof(bproto *) * (size + 1));
bproto **p = be_malloc(vm, sizeof(bproto *) * size);
memset(p, 0, sizeof(bproto *) * size);
proto->ptab = p;
proto->nproto = size;
while (size--) {
Expand Down
16 changes: 5 additions & 11 deletions lib/libesp32/berry/src/be_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ static void setupvals(bfuncinfo *finfo)
}

/* Function is complete, finalize bproto */
static void end_func(bparser *parser, bclass *c)
static void end_func(bparser *parser)
{
bvm *vm = parser->vm;
bfuncinfo *finfo = parser->finfo;
Expand All @@ -324,14 +324,8 @@ static void end_func(bparser *parser, bclass *c)
proto->codesize = finfo->pc;
proto->ktab = be_vector_release(vm, &finfo->kvec);
proto->nconst = be_vector_count(&finfo->kvec);
/* special case here */
proto->nproto = be_vector_count(&finfo->pvec);
if (proto->nproto == 0) {
proto->ptab = (void*) c;
} else {
be_vector_push_c(vm, &finfo->pvec, (void*) &c);
proto->ptab = be_vector_release(vm, &finfo->pvec);
}
proto->ptab = be_vector_release(vm, &finfo->pvec);
#if BE_USE_MEM_ALIGNED
proto->code = be_move_to_aligned(vm, proto->code, proto->codesize * sizeof(binstruction)); /* move `code` to 4-bytes aligned memory region */
proto->ktab = be_move_to_aligned(vm, proto->ktab, proto->nconst * sizeof(bvalue)); /* move `ktab` to 4-bytes aligned memory region */
Expand Down Expand Up @@ -644,7 +638,7 @@ static bproto* funcbody(bparser *parser, bstring *name, bclass *c, int type)
finfo.proto->varg |= BE_VA_STATICMETHOD;
}
stmtlist(parser); /* parse statement without final `end` */
end_func(parser, c); /* close function context */
end_func(parser); /* close function context */
match_token(parser, KeyEnd); /* skip 'end' */
return finfo.proto; /* return fully constructed `bproto` */
}
Expand Down Expand Up @@ -700,7 +694,7 @@ static void lambda_expr(bparser *parser, bexpdesc *e)
expr(parser, &e1);
check_var(parser, &e1);
be_code_ret(parser->finfo, &e1);
end_func(parser, NULL);
end_func(parser);
init_exp(e, ETPROTO, be_code_proto(parser->finfo, finfo.proto));
be_stackpop(parser->vm, 1);
}
Expand Down Expand Up @@ -1827,7 +1821,7 @@ static void mainfunc(bparser *parser, bclosure *cl)
cl->proto = finfo.proto;
be_remove(parser->vm, -3); /* pop proto from stack */
stmtlist(parser);
end_func(parser, NULL);
end_func(parser);
match_token(parser, TokenEOS); /* skip EOS */
}

Expand Down
67 changes: 5 additions & 62 deletions lib/libesp32/berry/src/be_solidifylib.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,6 @@ static void toidentifier(char *to, const char *p)
*to = 0; // final NULL
}

/* return the parent class of a function, encoded in ptab, or NULL if none */
static const bclass *m_solidify_get_parentclass(const bproto *pr)
{
const bclass *cla;
if (pr->nproto > 0) {
cla = (const bclass*) pr->ptab[pr->nproto];
} else {
cla = (const bclass*) pr->ptab;
}
if (cla && var_basetype(cla) == BE_CLASS) {
return cla;
} else {
return NULL;
}
}

static void m_solidify_bvalue(bvm *vm, bbool str_literal, const bvalue * value, const char *prefixname, const char *key, void* fout);

static void m_solidify_map(bvm *vm, bbool str_literal, bmap * map, const char *prefixname, void* fout)
Expand Down Expand Up @@ -261,15 +245,9 @@ static void m_solidify_bvalue(bvm *vm, bbool str_literal, const bvalue * value,
size_t id_len = toidentifier_length(func_name);
char func_name_id[id_len];
toidentifier(func_name_id, func_name);
/* get parent class name if any */
const bclass *parentclass = m_solidify_get_parentclass(clo->proto);
const char *parentclass_name = parentclass ? str(parentclass->name) : NULL;
const char *actualprefix = parentclass_name ? parentclass_name : prefixname;

logfmt("be_const_%sclosure(%s%s%s%s_closure)",
logfmt("be_const_%sclosure(%s%s%s_closure)",
var_isstatic(value) ? "static_" : "",
parentclass_name ? "class_" : "",
actualprefix ? actualprefix : "", actualprefix ? "_" : "",
prefixname ? prefixname : "", prefixname ? "_" : "",
func_name_id);
}
break;
Expand Down Expand Up @@ -354,10 +332,6 @@ static void m_solidify_proto_inner_class(bvm *vm, bbool str_literal, const bprot

static void m_solidify_proto(bvm *vm, bbool str_literal, const bproto *pr, const char * func_name, int indent, void* fout)
{
/* get parent class name if any */
const bclass *parentclass = m_solidify_get_parentclass(pr);
const char *parentclass_name = parentclass ? str(parentclass->name) : NULL;

logfmt("%*sbe_nested_proto(\n", indent, "");
indent += 2;

Expand All @@ -378,28 +352,18 @@ static void m_solidify_proto(bvm *vm, bbool str_literal, const bproto *pr, const

logfmt("%*s%d, /* has sup protos */\n", indent, "", (pr->nproto > 0) ? 1 : 0);
if (pr->nproto > 0) {
// if pr->nproto is not zero, we add a last value that is either NULL or the parent class
logfmt("%*s( &(const struct bproto*[%2d]) {\n", indent, "", pr->nproto + 1); /* one more slot */
logfmt("%*s( &(const struct bproto*[%2d]) {\n", indent, "", pr->nproto);
for (int32_t i = 0; i < pr->nproto; i++) {
size_t sub_len = strlen(func_name) + 10;
char sub_name[sub_len];
snprintf(sub_name, sizeof(sub_name), "%s_%"PRId32, func_name, i);
m_solidify_proto(vm, str_literal, pr->ptab[i], sub_name, indent+2, fout);
logfmt(",\n");
}
if (parentclass_name) {
logfmt("%*s&be_class_%s, \n", indent, "", parentclass_name);
} else {
logfmt("%*sNULL, \n", indent, "");
}
logfmt("%*s}),\n", indent, "");
} else {
if (parentclass_name) {
logfmt("%*s&be_class_%s, \n", indent, "", parentclass_name);
} else {
logfmt("%*sNULL, \n", indent, "");
}
}
logfmt("%*sNULL, /* no sub protos */\n", indent, "");
}

logfmt("%*s%d, /* has constants */\n", indent, "", (pr->nconst > 0) ? 1 : 0);
if (pr->nconst > 0) {
Expand Down Expand Up @@ -454,22 +418,6 @@ static void m_solidify_closure(bvm *vm, bbool str_literal, const bclosure *clo,
bproto *pr = clo->proto;
const char * func_name = str(pr->name);

/* get parent class name if any */
const bclass *parentclass = m_solidify_get_parentclass(pr);
const char *parentclass_name = parentclass ? str(parentclass->name) : NULL;
if (parentclass_name) {
/* check that the class name is the same as the prefix */
/* meaning that we are solidifying a method from its own class */
/* if they don't match, then the method is borrowed to another class and we don't export it */
char parentclass_prefix[strlen(parentclass_name) + 10];
snprintf(parentclass_prefix, sizeof(parentclass_prefix), "class_%s", parentclass_name);
if (strcmp(parentclass_prefix, prefixname) != 0) {
logfmt("// Borrowed method '%s' from class '%s'\n", func_name, parentclass_prefix);
logfmt("extern bclosure *%s_%s;\n", parentclass_prefix, func_name);
return;
}
}

if (clo->nupvals > 0) {
logfmt("--> Unsupported upvals in closure <---");
// be_raise(vm, "internal_error", "Unsupported upvals in closure");
Expand All @@ -484,11 +432,6 @@ static void m_solidify_closure(bvm *vm, bbool str_literal, const bclosure *clo,
logfmt("** Solidified function: %s\n", func_name);
logfmt("********************************************************************/\n");

if (parentclass_name) {
/* declare exten so we can have a pointer */
logfmt("extern const bclass be_class_%s;\n", parentclass_name);
}

{
size_t id_len = toidentifier_length(func_name);
char func_name_id[id_len];
Expand Down
15 changes: 0 additions & 15 deletions lib/libesp32/berry/tests/class.be
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,3 @@ assert(type(c4.c) == 'class')
c5 = c4.c()
assert(type(c5) == 'instance')
assert(classname(c5) == 'map')

#- classof now gets back the class of Berry methods -#
class A
def f() end
static def g() end
end
class B : A
def h() end
end
assert(classof(A.f) == A)
assert(classof(A.g) == A)
assert(classof(B.h) == B)
#- returns nil if native function of not in class -#
assert(classof(int) == nil)
assert(classof(def () end) == nil)
Loading

0 comments on commit bf7fbf2

Please sign in to comment.