Comments (7)
How could it ?
Example 1:
include mycustomSigaction
var a = Sigaction()
a.sa_handler = proc(x: cint) {.noconv.} = echo "catch", x
Output:
Error: undeclared field: 'sa_handler=' for type in.Sigaction [type declared in /usercode/in.nim(9, 6)]
Example 2:
include mycustomSigaction
var a = Sigaction()
a.internal.sa_handler = proc(x: cint) {.noconv.} = echo "catch", x
Output:
In file included from /usercode/nimcache/@min.nim.c:8:
/usercode/nimcache/@min.nim.c:43:40: error: expected ':', ',', ';', '}' or '__attribute__' before '.' token
43 | tyProc__KdSUXb6rBxR37qf0lYK7pQ sa_handler;
| ^~~~~~~~~~
/usercode/nimcache/@min.nim.c: In function 'NimMainModule':
/usercode/nimcache/@min.nim.c:172:50: error: 'struct sigaction' has no member named 'internal'
172 | nimlf_(15, "/usercode/in.nim"); a__in_u17.internal.sa_handler = colonanonymous___in_u18;
|
from nim.
Have you tested it ? ^^
I really didn't...
as in my current linux machine, the union is defined as an anonymous union...
It's really another big problem that different posix has different name for definition.
Okey, in C
sa_hander
is defined as a macro1 to something like__sigaction_handler.sa_handler
to support writing C code likes.sa_hanler
,
therefore the sa_handler
symbol is just polluted, leading code above no working.
For this case (and in fact for many other), such a workaround is invalid.
Footnotes
-
in fact in Nim we are using the similiar mechanism (via template/proc as getter/setter), but thanks to it that Nim's template can be typed and more flexible, such a problem won't be caused in Nim. ↩
from nim.
Why
type Sigaction_internal {.union.} = object
sa_handler*: proc (x: cint) {.noconv.}
sa_sigaction*: proc (x: cint, y: ptr SigInfo, z: pointer) {.noconv.}
type Sigaction* {.importc: "struct sigaction", header: "<signal.h>", final, pure.} = object
internal: Sigaction_internal
sa_mask*: Sigset
sa_flags*: cint
not working?
from nim.
I didn't realize it until now that
Nim's offsetof
is also mapped to C's operator.
The field access is mapped to C's too, instead of accessing via calculated offset, which is what I used to think.
Then I just cannot give a real-world example where the fields' order of a imported struct or whether they contains union matters.
e.g. Even though in my system, Uid
is of wrong size, (shall be 4, but 8 in Nim), nowadays, the code used to cause bug now works fine... (may because Nim used to calculate size itself, but now it uses C's sizeof
)
from nim.
@litlighilit don't know about future development on Nim's NIR or nlvm project. But if Nim's import are close to C definitions, this will make life easier on future. But I don't know more than you do
from nim.
type Sigaction_internal {.union.} = object
sa_handler*: proc (x: cint) {.noconv.}
sa_sigaction*: proc (x: cint, y: ptr SigInfo, z: pointer) {.noconv.}
type Sigaction* {.importc: "struct sigaction", header: "<signal.h>", final, pure.} = object
internal{.importc: "__sigaction_u".}: Sigaction_internal
sa_mask*: Sigset
sa_flags*: cint
But when it comes to anonymous union, there may be really no support yet...
So the {.union.}: ...
pragma statement can be introduced to solve this, but it shall be for anonymous union only.
Edit: not working for this case, see below for details.
from nim.
Have you tested it ? ^^
type Sigset* {.importc: "sigset_t", header: "<signal.h>", final, pure.} = object
abi: array[1024 div (8 * sizeof(culong)), culong]
type Sigaction_internal {.union.} = object
sa_handler*: proc (x: cint) {.noconv.}
sa_sigaction*: proc (x: cint, y: pointer, z: pointer) {.noconv.}
type Sigaction* {.importc: "struct sigaction", header: "<signal.h>", final, pure.} = object
internal{.importc: "__sigaction_u".}: Sigaction_internal
sa_mask*: Sigset
sa_flags*: cint
var a = Sigaction()
a.internal.sa_handler = proc(x: cint) {.noconv.} = echo "catch", x
Output
Hint: used config file '/playground/nim/config/nim.cfg' [Conf]
Hint: used config file '/playground/nim/config/config.nims' [Conf]
......................................................................
Hint: gcc -c -w -fmax-errors=3 -pthread -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@m..@splayground@snim@slib@[email protected] /usercode/nimcache/@m..@splayground@snim@slib@[email protected] [Exec]
Hint: gcc -c -w -fmax-errors=3 -pthread -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@m..@splayground@snim@slib@sstd@[email protected] /usercode/nimcache/@m..@splayground@snim@slib@sstd@[email protected] [Exec]
Hint: gcc -c -w -fmax-errors=3 -pthread -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@m..@splayground@snim@slib@[email protected] /usercode/nimcache/@m..@splayground@snim@slib@[email protected] [Exec]
Hint: gcc -c -w -fmax-errors=3 -pthread -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@m..@splayground@snim@[email protected] /usercode/nimcache/@m..@splayground@snim@[email protected] [Exec]
Hint: gcc -c -w -fmax-errors=3 -pthread -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c [Exec]
In file included from /usercode/nimcache/@min.nim.c:8:
/usercode/nimcache/@min.nim.c:42:40: error: expected ':', ',', ';', '}' or '__attribute__' before '.' token
42 | tyProc__KdSUXb6rBxR37qf0lYK7pQ sa_handler;
| ^~~~~~~~~~
/usercode/nimcache/@min.nim.c: In function 'NimMainModule':
/usercode/nimcache/@min.nim.c:171:50: error: 'struct sigaction' has no member named '__sigaction_u'
171 | nimlf_(14, "/usercode/in.nim"); a__in_u16.__sigaction_u.sa_handler = colonanonymous___in_u17;
|
And even if it worked, having to do a.internal.sa_handler
whereas in c, it is a.sa_handler
is a problematic workaround IMHO
from nim.
Related Issues (20)
- Using waitForExit with timeout from multiple threads simultaneously hangs HOT 1
- Cyclic proc calls causes infinite memory usage HOT 12
- Custom iterator is not found if used in template function, if used from a third file HOT 1
- bisecting 2.2.0 rc1 HOT 17
- cursor optimizer doesn't understand that distinct cstring can be an owning type
- Cross compilation with mingw for cpu=i386 from linux/macos fails with time_t bug HOT 2
- Type error when sink added to parameters HOT 2
- Nim `devel` nightly `i386` build failing HOT 1
- `koch pdf` produces a warning about unknown substitution 'nimversion'
- `nim gendepend` does not work with var-less destructors
- When JS, operations on pointer casted from ref object raises Runtime-Errors
- Regression - cannot instantiate Maybe generic arguments are missing HOT 5
- Identifier expected or compiler segfault with templates over generic type in type section HOT 3
- Nested generic/static parameter: undeclared identifier in typesections HOT 2
- 2.2.0 rc1 regression with cdecl functions
- Warnings missing when compiling the same file twice HOT 4
- `genericsOpenSym` not working when additional generally non-matching symbol with same name is present HOT 2
- nimsuggest crashes when using `--mm:arc/orc` and if an object that contains `string` or `seq` is constructed HOT 2
- ARC hooks missing when sinking generic typeclass HOT 3
- Generic concepts not compatible with openarray HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nim.