v.embedfile: make path public (#12614)

pull/12618/head
Leo Developer 2021-11-29 15:32:42 +01:00 committed by GitHub
parent ddec89f9ee
commit 0f59d88ba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 44 additions and 6 deletions

View File

@ -2,10 +2,13 @@ module embed_file
import os
pub const (
is_used = 1
)
// https://github.com/vlang/rfcs/blob/master/embedding_resources.md
// EmbedFileData encapsulates functionality for the `$embed_file()` compile time call.
pub struct EmbedFileData {
path string
apath string
mut:
compressed &byte
@ -13,11 +16,12 @@ mut:
free_compressed bool
free_uncompressed bool
pub:
len int
len int
path string
}
pub fn (ed EmbedFileData) str() string {
return 'embed_file.EmbedFileData{ len: $ed.len, path: "$ed.path", path: "$ed.apath", uncompressed: ${ptr_str(ed.uncompressed)} }'
return 'embed_file.EmbedFileData{ len: $ed.len, path: "$ed.path", apath: "$ed.apath", uncompressed: ${ptr_str(ed.uncompressed)} }'
}
[unsafe]

View File

@ -6,6 +6,7 @@ fn test_const_embed_file() {
assert file.len == 603
fdata := file.data()
eprintln('file after .data() call: $file')
assert file.path == 'v.png'
assert file.len == 603
unsafe {
assert fdata.vbytes(4) == [byte(0x89), `P`, `N`, `G`]

View File

@ -0,0 +1,18 @@
import v.embed_file
fn test_embed_file_with_import() {
mut file := $embed_file('v.png')
eprintln('file: $file')
assert file.len == 603
fdata := file.data()
eprintln('file after .data() call: $file')
assert file.len == 603
unsafe {
assert fdata.vbytes(4) == [byte(0x89), `P`, `N`, `G`]
}
assert check_file(file)
}
fn check_file(file embed_file.EmbedFileData) bool {
return file.len == 603
}

View File

Before

Width:  |  Height:  |  Size: 603 B

After

Width:  |  Height:  |  Size: 603 B

View File

@ -98,7 +98,7 @@ fn prepare_bin2v_file(mut fmt_bench benchmark.Benchmark) {
}
fn write_bin2v_keep_content() ? {
img0 := os.join_path('vlib', 'v', 'embed_file', 'v.png')
img0 := os.join_path('vlib', 'v', 'embed_file', 'tests', 'v.png')
img1 := os.join_path('tutorials', 'building_a_simple_web_blog_with_vweb', 'img', 'time.png')
os.rm(b2v_keep_path) ?
res := os.execute('$vexe bin2v -w $b2v_keep_path $img0 $img1')

View File

@ -14,7 +14,12 @@ fn (mut g Gen) embed_file_is_prod_mode() bool {
fn (mut g Gen) gen_embed_file_init(node ast.ComptimeCall) {
g.writeln('(v__embed_file__EmbedFileData){')
g.writeln('\t\t.path = ${ctoslit(node.embed_file.rpath)},')
g.writeln('\t\t.apath = ${ctoslit(node.embed_file.apath)},')
if g.embed_file_is_prod_mode() {
// apath is not needed in production and may leak information
g.writeln('\t\t.apath = ${ctoslit('')},')
} else {
g.writeln('\t\t.apath = ${ctoslit(node.embed_file.apath)},')
}
file_size := os.file_size(node.embed_file.apath)
if file_size > 5242880 {
eprintln('Warning: embedding of files >= ~5MB is currently not supported')

View File

@ -26,6 +26,7 @@ v__embed_file__EmbedFileIndexEntry* v__embed_file__find_index_entry_by_path(void
v__embed_file__EmbedFileData my_source = (v__embed_file__EmbedFileData){
.path = _SLIT("embed.vv"),
.apath = _SLIT(""),
.compressed = v__embed_file__find_index_entry_by_path((voidptr)_v_embed_file_index, _SLIT("embed.vv"))->data,
// Initializations for module v.embed_file :

View File

@ -125,7 +125,7 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall {
epath = abs_path
}
}
p.register_auto_import('v.embed_file')
p.register_auto_import('v.preludes.embed_file')
return ast.ComptimeCall{
scope: 0
is_embed: true

View File

@ -0,0 +1,9 @@
module embed_file
// This prelude is loaded in every v program that uses `$embed_file`,
// in both the main executable, and in the shared library.
import v.embed_file
const (
no_warning_embed_file_is_used = embed_file.is_used
)