diff --git a/vlib/v/embed_file/embed_file.v b/vlib/v/embed_file/embed_file.v index 3c556d343c..22ea45807e 100644 --- a/vlib/v/embed_file/embed_file.v +++ b/vlib/v/embed_file/embed_file.v @@ -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] diff --git a/vlib/v/embed_file/embed_file_test.v b/vlib/v/embed_file/tests/embed_file_test.v similarity index 95% rename from vlib/v/embed_file/embed_file_test.v rename to vlib/v/embed_file/tests/embed_file_test.v index 2af4459ea4..f3cc8223af 100644 --- a/vlib/v/embed_file/embed_file_test.v +++ b/vlib/v/embed_file/tests/embed_file_test.v @@ -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`] diff --git a/vlib/v/embed_file/tests/embed_file_test_with_import.v b/vlib/v/embed_file/tests/embed_file_test_with_import.v new file mode 100644 index 0000000000..1db2ff6f18 --- /dev/null +++ b/vlib/v/embed_file/tests/embed_file_test_with_import.v @@ -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 +} diff --git a/vlib/v/embed_file/v.png b/vlib/v/embed_file/tests/v.png similarity index 100% rename from vlib/v/embed_file/v.png rename to vlib/v/embed_file/tests/v.png diff --git a/vlib/v/fmt/fmt_keep_test.v b/vlib/v/fmt/fmt_keep_test.v index be00fa9750..a26897d2df 100644 --- a/vlib/v/fmt/fmt_keep_test.v +++ b/vlib/v/fmt/fmt_keep_test.v @@ -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') diff --git a/vlib/v/gen/c/embed.v b/vlib/v/gen/c/embed.v index 635563f7ad..9ecb9bc415 100644 --- a/vlib/v/gen/c/embed.v +++ b/vlib/v/gen/c/embed.v @@ -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') diff --git a/vlib/v/gen/c/testdata/embed_with_prod.c.must_have b/vlib/v/gen/c/testdata/embed_with_prod.c.must_have index 276917108e..8c96f2a788 100644 --- a/vlib/v/gen/c/testdata/embed_with_prod.c.must_have +++ b/vlib/v/gen/c/testdata/embed_with_prod.c.must_have @@ -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 : diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index af2659fc9f..3c6a0a8574 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -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 diff --git a/vlib/v/preludes/embed_file/embed_file.v b/vlib/v/preludes/embed_file/embed_file.v new file mode 100644 index 0000000000..afe0eb10bb --- /dev/null +++ b/vlib/v/preludes/embed_file/embed_file.v @@ -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 +)