-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add reflect.Type.ConvertibleTo() #5422
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
Open
dgryski
wants to merge
10
commits into
tinygo-org:dev
Choose a base branch
from
dgryski:dgryski/convertible-to
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4a66c27
reflect: add Type.ConvertibleTo
dgryski 35e2b9e
reflect: add converting complex
dgryski dd6cc1a
reflect: add some more complex conversion tests
dgryski 210bfd5
reflect: add string <-> []rune conversions
dgryski 45b7536
reflect: add (u)int -> string conversions
dgryski afd6df4
reflect: fix (u)int -> string conversions for invalid code points
dgryski 9c46384
refleect: more conversion tests
dgryski 552e9b0
reflect: use TestConvert from all_test.go and make it pass
dgryski ec6264b
reflect: address Copilot review feedback
dgryski 640360f
reflect: fix pointer receiver for MakeRO
dgryski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,12 +47,20 @@ func (v Value) isExported() bool { | |
| return v.flags&valueFlagExported != 0 | ||
| } | ||
|
|
||
| func (v Value) isRO() bool { | ||
| func (v Value) IsRO() bool { | ||
| return v.flags&(valueFlagRO) != 0 | ||
| } | ||
|
|
||
| func (v *Value) MakeRO(ro bool) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to be a pointer receiver here? None of the other functions are.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. |
||
| if ro { | ||
| v.flags |= valueFlagRO | ||
| } else { | ||
| v.flags &^= valueFlagRO | ||
| } | ||
| } | ||
|
|
||
| func (v Value) checkRO() { | ||
| if v.isRO() { | ||
| if v.IsRO() { | ||
| panic("reflect: value is not settable") | ||
| } | ||
| } | ||
|
|
@@ -297,7 +305,7 @@ func (v Value) IsValid() bool { | |
| } | ||
|
|
||
| func (v Value) CanInterface() bool { | ||
| return v.isExported() && !v.isRO() | ||
| return v.isExported() && !v.IsRO() | ||
| } | ||
|
|
||
| func (v Value) CanAddr() bool { | ||
|
|
@@ -1485,13 +1493,11 @@ func convertOp(src Value, typ Type) (Value, bool) { | |
| return cvtFloat(src, rtype), true | ||
| } | ||
|
|
||
| /* | ||
| case Complex64, Complex128: | ||
| switch src.Kind() { | ||
| case Complex64, Complex128: | ||
| return cvtComplex | ||
| } | ||
| */ | ||
| case Complex64, Complex128: | ||
| switch src.Kind() { | ||
| case Complex64, Complex128: | ||
| return cvtComplex(src, typ.(*RawType)), true | ||
| } | ||
|
dgryski marked this conversation as resolved.
|
||
|
|
||
| case Slice: | ||
| switch rtype := typ.(*RawType); rtype.Kind() { | ||
|
|
@@ -1534,6 +1540,12 @@ func convertOp(src Value, typ Type) (Value, bool) { | |
| return cvtStringRunes(src, rtype), true | ||
| } | ||
| } | ||
|
|
||
| case Pointer: | ||
| rtype := typ.(*RawType) | ||
| if rtype.Kind() == Pointer && !rtype.isNamed() { | ||
| return cvtDirect(src, rtype), true | ||
| } | ||
|
dgryski marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // TODO(dgryski): Unimplemented: | ||
|
|
@@ -1578,6 +1590,18 @@ func cvtFloat(v Value, t *RawType) Value { | |
| return makeFloat(v.flags, v.Float(), t) | ||
| } | ||
|
|
||
| func cvtDirect(v Value, t *RawType) Value { | ||
| return Value{ | ||
| typecode: t, | ||
| value: v.value, | ||
| flags: v.flags, | ||
| } | ||
| } | ||
|
|
||
| func cvtComplex(v Value, t *RawType) Value { | ||
| return makeComplex(v.flags, v.Complex(), t) | ||
| } | ||
|
|
||
| //go:linkname stringToBytes runtime.stringToBytes | ||
| func stringToBytes(x string) []byte | ||
|
|
||
|
|
@@ -1661,20 +1685,79 @@ func makeFloat32(flags valueFlags, f float32, t *RawType) Value { | |
| return v | ||
| } | ||
|
|
||
| func cvtIntString(src Value, t *RawType) Value { | ||
| panic("cvtUintString: unimplemented") | ||
| func makeComplex(flags valueFlags, f complex128, t *RawType) Value { | ||
| size := t.Size() | ||
|
|
||
| v := Value{ | ||
| typecode: t, | ||
| flags: flags, | ||
| } | ||
|
|
||
| ptr := unsafe.Pointer(&v.value) | ||
| if size > unsafe.Sizeof(uintptr(0)) { | ||
| ptr = alloc(size, nil) | ||
| v.value = ptr | ||
| } | ||
|
|
||
| switch size { | ||
| case 8: | ||
| *(*complex64)(ptr) = complex64(f) | ||
| case 16: | ||
| *(*complex128)(ptr) = f | ||
| } | ||
| return v | ||
| } | ||
|
|
||
| //go:linkname stringFromUnicode runtime.stringFromUnicode | ||
| func stringFromUnicode(x rune) string | ||
|
dgryski marked this conversation as resolved.
Outdated
|
||
|
|
||
| func cvtIntString(v Value, t *RawType) Value { | ||
| s := "\uFFFD" | ||
| if x := v.Int(); int64(rune(x)) == x { | ||
| s = string(rune(x)) | ||
| } | ||
| return Value{ | ||
| typecode: t, | ||
| value: unsafe.Pointer(&s), | ||
| flags: v.flags, | ||
| } | ||
| } | ||
|
|
||
| func cvtUintString(src Value, t *RawType) Value { | ||
| panic("cvtUintString: unimplemented") | ||
| func cvtUintString(v Value, t *RawType) Value { | ||
| s := "\uFFFD" | ||
| if x := v.Uint(); uint64(rune(x)) == x { | ||
| s = string(rune(x)) | ||
| } | ||
|
|
||
| return Value{ | ||
| typecode: t, | ||
| value: unsafe.Pointer(&s), | ||
| flags: v.flags, | ||
| } | ||
| } | ||
|
|
||
| func cvtStringRunes(src Value, t *RawType) Value { | ||
| panic("cvsStringRunes: unimplemented") | ||
| //go:linkname stringToRunes runtime.stringToRunes | ||
| func stringToRunes(s string) []rune | ||
|
|
||
| func cvtStringRunes(v Value, t *RawType) Value { | ||
| b := stringToRunes(*(*string)(v.value)) | ||
| return Value{ | ||
| typecode: t, | ||
| value: unsafe.Pointer(&b), | ||
| flags: v.flags, | ||
| } | ||
| } | ||
|
|
||
| func cvtRunesString(src Value, t *RawType) Value { | ||
| panic("cvsRunesString: unimplemented") | ||
| //go:linkname stringFromRunes runtime.stringFromRunes | ||
| func stringFromRunes(r []rune) string | ||
|
|
||
| func cvtRunesString(v Value, t *RawType) Value { | ||
| s := stringFromRunes(*(*[]rune)(v.value)) | ||
| return Value{ | ||
| typecode: t, | ||
| value: unsafe.Pointer(&s), | ||
| flags: v.flags, | ||
| } | ||
| } | ||
|
|
||
| //go:linkname slicePanic runtime.slicePanic | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.