-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add read_file
macro method
#2791
Conversation
Forgot to mention that this works more or less like the system call: one has to use |
Nim is another language that has this feature: https://github.com/nim-lang/Nim/blob/master/lib/system.nim#L3262 , and the reason is embedding resources too. |
To be honest I'm not sure we should do this before it works for any kind of data. Returning We currently don't support a platform where the |
https://github.com/graphitemaster/incbin shows an interesting approach, but sadly LLVM says no: asm(%(
.section .rodata
.global gBlobData
.type gBlobData, @object
.balign 16
gBlobData:
.incbin "hello.cr"
.global gBlobEnd
.type gBlobEnd, @object
.balign 1
gBlobEnd:
.byte 1
.global gBlobSize
.type gBlobSize, @object
.balign 16
gBlobSize:
.int gBlobEnd - gBlobData
))
lib LibEmbed
$gBlobData : LibC::Char*
$gBlobSize : LibC::UInt
end
pp LibEmbed.gBlobSize
pp LibEmbed.gBlobData
blob = Slice.new(LibEmbed.gBlobData, 1)
pp blob
|
Bump. |
Just use |
Great preparation for Windows support. |
@asterite And what about reasons you've given in point no. 2? Also, I'd say that closing PR with 6 x |
@Sija With |
@asterite I think you meant |
Any chance of this or something similar being revived? It'd be nice to have a platform independent/shell-free way to embed data in a macro. |
You might want to take a look at schovi/baked_file_system. |
Thanks @straight-shoota! I saw that in #1649 too. I'd love to see this become part of the language at some point, but |
Could this PR be revaluated once more? @asterite come to your senses, please! ;) |
@jhass the reason LLVM will complain is that |
Cool, so let's do that! |
Check the diff for the docs.
The rationale for introducing this is that:
cat filename.txt
but that's not portable, and one has to redirect errors to avoid getting a compilation error.read_file
returnsnil
if the file doesn't exist. With that, one can choose to deal withnil
at compile time or runtime.This works for text files, but one can also use it for binary data: simply callto_slice
afterwards to get the bytes (though we probably need a literal for binary data, but that's another issue)Actually, it doesn't work for binary files because the string won't be able to be embedded at all. We could probably add a
read_binary_file
later, if we really want to do that.Fixes #1649 and #2751