Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starlark: @StarlarkMethod.trustReturnsValid #13012

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Starlark: @StarlarkMethod.trustReturnsValid
When this parameter is specified for method, Starlark interpreter
assumes function returns a valid Starlark value.

When method is declared to return `Object`, Starlark interpreter
by default checks that object is a valid Starlark value, which is
quite expensive.
  • Loading branch information
stepancheg committed May 2, 2022
commit 898fbdef9996b41e40b03048cb1ffb09033b24cb
6 changes: 6 additions & 0 deletions src/main/java/net/starlark/java/annot/StarlarkMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@
*/
boolean allowReturnNones() default false;

/**
* If true, for a method which returns any type not known to be a valid Starlark value,
* the Starlark interpreter does not validate that the returned object is a legal Starlark value.
*/
boolean trustReturnsValid() default false;

/**
* If true, the StarlarkThread will be passed as an argument of the annotated function. (Thus, the
* annotated method signature must contain StarlarkThread as a parameter. See the interface-level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private MethodDescriptor(
boolean extraKeywords,
boolean selfCall,
boolean allowReturnNones,
boolean trustReturnsValid,
boolean useStarlarkThread,
boolean useStarlarkSemantics) {
this.method = method;
Expand All @@ -92,7 +93,8 @@ private MethodDescriptor(
howToHandleReturn = HowToHandleReturn.NULL_TO_NONE;
} else if (StarlarkValue.class.isAssignableFrom(ret)
|| String.class == ret
|| Boolean.class == ret) {
|| Boolean.class == ret
|| trustReturnsValid) {
howToHandleReturn =
allowReturnNones ? HowToHandleReturn.NULL_TO_NONE : HowToHandleReturn.ERROR_ON_NULL;
} else if (ret == int.class) {
Expand Down Expand Up @@ -131,6 +133,7 @@ static MethodDescriptor of(
!annotation.extraKeywords().name().isEmpty(),
annotation.selfCall(),
annotation.allowReturnNones(),
annotation.trustReturnsValid(),
annotation.useStarlarkThread(),
annotation.useStarlarkSemantics());
}
Expand Down