Skip to content

Commit

Permalink
Fix field visibility in objectHas for extended objects (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkmik committed Jun 10, 2024
1 parent c159b34 commit 94a40b2
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 9 deletions.
5 changes: 4 additions & 1 deletion builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,10 @@ func builtinObjectHasEx(i *interpreter, objv value, fnamev value, includeHiddenV
return nil, err
}
h := withHiddenFromBool(includeHidden.value)
hasField := objectHasField(objectBinding(obj), string(fname.getRunes()), h)

hide, hasField := objectFieldsVisibility(obj)[string(fname.getRunes())]
hasField = hasField && (h == withHidden || hide != ast.ObjectFieldHidden)

return makeValueBoolean(hasField), nil
}

Expand Down
2 changes: 1 addition & 1 deletion interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ func (i *interpreter) rawevaluate(a ast.Node, tc tailCallStatus) (value, error)
if err != nil {
return nil, err
}
hasField := objectHasField(i.stack.getSelfBinding().super(), indexStr.getGoString(), withHidden)
hasField := objectHasField(i.stack.getSelfBinding().super(), indexStr.getGoString())
return makeValueBoolean(hasField), nil

case *ast.Function:
Expand Down
1 change: 1 addition & 0 deletions testdata/std.objectHasEx5.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/std.objectHasEx5.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.objectHasEx({"x":: null} + {"x": null}, "x", false)
Empty file.
2 changes: 2 additions & 0 deletions testdata/stdlib_smoke_test.golden
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
"format": "test blah 42",
"get": [
17,
42,
18,
42
],
"isArray": true,
Expand Down
2 changes: 2 additions & 0 deletions testdata/stdlib_smoke_test.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
get: [
std.get(o={a:: 17}, f="a"),
std.get(o={a:: 17}, f="a", default=42, inc_hidden=false),
std.get(o={a:: 17} + {a: 18}, f="a", default=42),
std.get(o={a:: 17} + {a: 18}, f="a", default=42, inc_hidden=false),
],

// isSomething
Expand Down
2 changes: 1 addition & 1 deletion thunks.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (f *plusSuperUnboundField) evaluate(i *interpreter, sb selfBinding, origBin
return nil, err
}

if !objectHasField(sb.super(), fieldName, withHidden) {
if !objectHasField(sb.super(), fieldName) {
return right, nil
}

Expand Down
9 changes: 3 additions & 6 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,12 +730,9 @@ func objectIndex(i *interpreter, sb selfBinding, fieldName string) (value, error
return val, err
}

func objectHasField(sb selfBinding, fieldName string, h hidden) bool {
found, field, _, _, _ := findField(sb.self.uncached, sb.superDepth, fieldName)
if !found || (h == withoutHidden && field.hide == ast.ObjectFieldHidden) {
return false
}
return true
func objectHasField(sb selfBinding, fieldName string) bool {
found, _, _, _, _ := findField(sb.self.uncached, sb.superDepth, fieldName)
return found
}

type fieldHideMap map[string]ast.ObjectFieldHide
Expand Down

0 comments on commit 94a40b2

Please sign in to comment.