Merge pull request #16383 from s-hadinger/berry_stricter

Berry stricter strict mode
This commit is contained in:
s-hadinger 2022-08-29 19:40:51 +02:00 committed by GitHub
commit 286b0ca916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View File

@ -469,6 +469,12 @@ static int new_upval(bvm *vm, bfuncinfo *finfo, bstring *name, bexpdesc *var)
static void new_var(bparser *parser, bstring *name, bexpdesc *var)
{
bfuncinfo *finfo = parser->finfo;
if (comp_is_strict(parser->vm)) {
/* check if we are masking a builtin */
if (be_builtin_class_find(parser->vm, name) >= 0) {
push_error(parser, "strict: redefinition of builtin '%s'", str(name));
}
}
if (finfo->prev || finfo->binfo->prev || parser->islocal) {
init_exp(var, ETLOCAL, 0);
var->v.idx = new_localvar(parser, name); /* if local, contains the index in current local var list */
@ -982,7 +988,6 @@ static void compound_assign(bparser *parser, int op, bexpdesc *l, bexpdesc *r)
/* A new implicit local variable is created if no global has the same name (excluding builtins) */
/* This means that you can override a builtin silently */
/* This also means that a function cannot create a global, they must preexist or create with `global` module */
/* TODO add warning in strict mode */
static int check_newvar(bparser *parser, bexpdesc *e)
{
if (e->type == ETGLOBAL) {

View File

@ -132,6 +132,21 @@ int be_builtin_find(bvm *vm, bstring *name)
return -1; /* not found */
}
/* find in the list of builtins or classes - used by strict to avoid accidental change of those */
int be_builtin_class_find(bvm *vm, bstring *name)
{
bvalue *res = be_map_findstr(vm, builtin(vm).vtab, name);
if (res) {
return var_toidx(res);
} else {
int idx = global_native_class_find(vm, name);
if (idx >= 0) {
return idx;
}
}
return -1; /* not found */
}
bstring* be_builtin_name(bvm *vm, int index)
{
bmap *map = builtin(vm).vtab;

View File

@ -23,6 +23,7 @@ int be_global_new(bvm *vm, bstring *name);
bvalue* be_global_var(bvm *vm, int index);
void be_global_release_space(bvm *vm);
int be_builtin_find(bvm *vm, bstring *name);
int be_builtin_class_find(bvm *vm, bstring *name);
bstring* be_builtin_name(bvm *vm, int index);
int be_builtin_new(bvm *vm, bstring *name);
void be_bulitin_release_space(bvm *vm);