-
Notifications
You must be signed in to change notification settings - Fork 22
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
Use with threads and gcsafe code ? #28
Comments
That's a good one, and I never gave it a thought. In theory I'd say that there is no reason why this should not work, as there is no global state involved. I'll try if I can spend some time to see why Nim thinks this is a problem. |
Hm, I just tried the the brute-force approach of adding
This is funny, because when your parser is const, the call is not really indirect. I'll see if I can find a way to tell the compiler "trust me I know what I'm doing", or discuss this with some wise people on the #nim channel. |
When you make indirect calls, function pointers and closures, the closure must be annotated with |
Sure, the closure also gets the pragma. My current problem is that it is perfectly valid to access globals from parsers if you want to, but if I would mark the generated closure as |
Yes, that's a problem. Perhaps the generated code can be templated on the closure type and allow two closures types, one with gcsafe and the other without. If the rest of the code is actually gcsafe, its status will be correctly inferred. Araq pointed me to nim-lang/RFCs#142 after https://forum.nim-lang.org/t/7110#44827 |
I think I can make a relatively simple fix for this. Just to make sure I'm looking at the right thing, can you share a minimal complete example that shows your problem? |
Minimal test case: import npeg, strutils
const qdata = peg("data", res: string):
hex <- {'a'..'f'} | {'A'..'F'} | {'0'..'9'}
enc_byte <- '=' * >(hex * hex):
res = res & parseHexStr($1)
underscore <- >"_":
res = res & " "
char <- >(1):
res = res & $1
data <- *( enc_byte | underscore | char )
proc testnpeg() {.gcsafe.} =
var result = ""
if not qdata.match("Hello=20World", result).ok:
echo "Failed to decode"
else:
echo result
testnpeg() Compiler messages:
With nim 1.4.0 |
Right; I made a I wonder if we can make this automatically inferred somehow... |
Thank you, I did not expect a quick response like that. I don't know how it can be made easier (you may want to use gcsafe peg in some portion of your code and not gcsafe in another), but this is already helping. I believe Nim will have to think about what it will want to do with gcsafe. |
Merged and released in 0.24.0 |
I still get an error after applying the compiler argument to force gcsafe. Before the argument, I get the expected failure:
But after adding it:
It seems like the generated proc is not getting the annotation that is required. My code follows a similar pattern from the example above: PEG is a type
STS5Config = object
filterId: Option[string]
minConstExpr: int = 3
const SafeTS5 = peg("query", userdata: STS5Config):
# ... That is called from another proc ( |
Here's a workaround which is a bit hacky. I needed to:
An alternative to the second point is to wrap calls in {. gcsafe .}:
var ms = p.fn_init()
p.fn_run(ms, s, userData) Weirdly, the actual flag provided here |
Hmm, npeg has seen some major refactorigns since the |
(FYI, I'm using Nim 2, where ORC, threads is default) |
Hi,
I am wondering how to use npeg with gcsafe code or threads. i declared my grammar constant, but I get errors like this when using the grammar
match
procedure:Is it possible to use npeg with gcsafe code ?
The text was updated successfully, but these errors were encountered: