|
2 | 2 | // Use of this source code is governed by a BSD 3-Clause |
3 | 3 | // license that can be found in the LICENSE file. |
4 | 4 |
|
| 5 | +use "std/conv" |
5 | 6 | use "std/fmt" |
6 | 7 | use "std/internal/jule/mod" |
7 | 8 | use "std/jule/ast" |
8 | 9 | use "std/jule/constant" |
9 | 10 | use "std/jule/log" |
10 | 11 | use "std/jule/types" |
| 12 | +use "std/os/filepath" |
11 | 13 |
|
12 | 14 | // Type alias for built-in function callers. |
13 | 15 | // |
@@ -1290,6 +1292,71 @@ fn builtinCallerStdComptimeTypeAlias(mut e: &eval, mut fc: &ast::CallExpr, _: &V |
1290 | 1292 | ret buildVoidValue() |
1291 | 1293 | } |
1292 | 1294 |
|
| 1295 | +fn builtinCallerStdComptimeIncludeBytes(mut e: &eval, mut fc: &ast::CallExpr, _: &Value): &Value { |
| 1296 | + if len(fc.Args) > 1 { |
| 1297 | + e.pushErr(fc.Token, "passed more argument than expected to IncludeBytes") |
| 1298 | + e.pushSuggestion("call like; IncludeBytes(Path)") |
| 1299 | + ret nil |
| 1300 | + } |
| 1301 | + if len(fc.Args) == 0 { |
| 1302 | + e.pushErr(fc.Token, "identifier and type is missing for IncludeBytes") |
| 1303 | + e.pushSuggestion("call like; IncludeBytes(Path)") |
| 1304 | + ret nil |
| 1305 | + } |
| 1306 | + mut v := e.eval(fc.Args[0], evalDefault) |
| 1307 | + if v == nil { |
| 1308 | + ret nil |
| 1309 | + } |
| 1310 | + if !v.IsConst() || !v.Constant.IsStr() { |
| 1311 | + e.pushErr(fc.Args[0].Token, "expression must be constant and string") |
| 1312 | + ret nil |
| 1313 | + } |
| 1314 | + mut path := v.Constant.ReadStr() |
| 1315 | + if len(path) == 0 { |
| 1316 | + e.pushErr(fc.Args[0].Token, "empty path for IncludeBytes") |
| 1317 | + ret nil |
| 1318 | + } |
| 1319 | + if !filepath::IsAbs(path) { |
| 1320 | + packagefile := e.s.getCurrentFile() |
| 1321 | + packagedir := filepath::Dir(packagefile.File.Path) |
| 1322 | + path = filepath::Join(packagedir, path) |
| 1323 | + path = filepath::Abs(path) else { |
| 1324 | + e.pushErr(fc.Args[0].Token, "path handling failed: "+fmt::Sprint(error)) |
| 1325 | + ret nil |
| 1326 | + } |
| 1327 | + } |
| 1328 | + mut file := e.s.importer.GetFile(path) |
| 1329 | + if file == nil { |
| 1330 | + file = e.s.importer.IncludeFile(path) |
| 1331 | + if file == nil { |
| 1332 | + e.pushErr(fc.Args[0].Token, "IncludeBytes cannot read file: "+conv::Quote(path)) |
| 1333 | + ret nil |
| 1334 | + } |
| 1335 | + e.s.importer.IncludedFile(file) |
| 1336 | + } |
| 1337 | + data := file.Bytes() |
| 1338 | + mut bytes := &SliceExpr{ |
| 1339 | + ElemType: primU8, |
| 1340 | + Elems: make([]&Value, 0, len(data)), |
| 1341 | + } |
| 1342 | + for _, b in data { |
| 1343 | + mut bc := constant::NewU64(u64(b)) |
| 1344 | + bytes.Elems = append(bytes.Elems, &Value{ |
| 1345 | + Type: &Type{Kind: primU8}, |
| 1346 | + Constant: bc, |
| 1347 | + Model: bc, |
| 1348 | + }) |
| 1349 | + } |
| 1350 | + ret &Value{ |
| 1351 | + Type: &Type{ |
| 1352 | + Kind: &Slice{ |
| 1353 | + Value: primU8, |
| 1354 | + }, |
| 1355 | + }, |
| 1356 | + Model: bytes, |
| 1357 | + } |
| 1358 | +} |
| 1359 | + |
1293 | 1360 | fn builtinCallerStdMemSizeOf(mut e: &eval, mut fc: &ast::CallExpr, _: &Value): &Value { |
1294 | 1361 | mut result := &Value{ |
1295 | 1362 | Type: primUint, |
@@ -1367,7 +1434,7 @@ fn builtinCallerStdIntegEmit(mut e: &eval, mut fc: &ast::CallExpr, mut v: &Value |
1367 | 1434 | } |
1368 | 1435 |
|
1369 | 1436 | if !argVal.IsConst() || !argVal.Constant.IsStr() { |
1370 | | - e.pushErr(fc.Args[0].Token, "expression must be constant") |
| 1437 | + e.pushErr(fc.Args[0].Token, "expression must be constant and string") |
1371 | 1438 | ret nil |
1372 | 1439 | } |
1373 | 1440 |
|
@@ -1496,6 +1563,7 @@ fn init() { |
1496 | 1563 | builtinFuncsStdComptime["File"] = &FuncIns{caller: builtinCallerStdComptimeFile} |
1497 | 1564 | builtinFuncsStdComptime["Files"] = &FuncIns{caller: builtinCallerStdComptimeFiles} |
1498 | 1565 | builtinFuncsStdComptime["TypeAlias"] = &FuncIns{caller: builtinCallerStdComptimeTypeAlias} |
| 1566 | + builtinFuncsStdComptime["IncludeBytes"] = &FuncIns{caller: builtinCallerStdComptimeIncludeBytes} |
1499 | 1567 |
|
1500 | 1568 | // Initialize built-in functions of the "std/integ" package. |
1501 | 1569 | builtinFuncsStdInteg["Emit"] = &FuncIns{ |
|
0 commit comments