|
1 | 1 | package org.piccode.rt.modules; |
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.util.HashMap; |
3 | 5 | import java.util.List; |
4 | 6 | import org.piccode.rt.Context; |
| 7 | +import org.piccode.rt.PiccodeBoolean; |
5 | 8 | import org.piccode.rt.PiccodeNumber; |
| 9 | +import org.piccode.rt.PiccodeObject; |
| 10 | +import org.piccode.rt.PiccodeString; |
6 | 11 | import org.piccode.rt.PiccodeValue; |
7 | 12 | import org.piccode.rt.PiccodeValue.Type; |
8 | 13 |
|
9 | | - |
10 | 14 | /** |
11 | 15 | * |
12 | 16 | * @author hexaredecimal |
13 | 17 | */ |
14 | 18 | public class PiccodeFileModule { |
| 19 | + |
| 20 | + private static PiccodeValue makeFile(File fp) { |
| 21 | + var obj = new HashMap<String, PiccodeValue>(); |
| 22 | + obj.put("hash", new PiccodeNumber(fp.hashCode())); |
| 23 | + obj.put("path", new PiccodeString(fp.getPath())); |
| 24 | + obj.put("parent", new PiccodeString(fp.getParent())); |
| 25 | + obj.put("exists", new PiccodeBoolean(fp.exists())); |
| 26 | + obj.put("file", new PiccodeBoolean(fp.isFile())); |
| 27 | + obj.put("hidden", new PiccodeBoolean(fp.isHidden())); |
| 28 | + return new PiccodeObject(obj); |
| 29 | + } |
| 30 | + |
15 | 31 | public static void addFunctions() { |
16 | | - |
17 | | - NativeFunctionFactory.create("new", List.of("path"), (args, namedArgs, frame) -> { |
18 | | - var ctx = frame == null ? |
19 | | - Context.top |
20 | | - : Context.getContextAt(frame); |
21 | | - |
22 | | - var scope = ctx.getTopFrame(); |
23 | | - var ms = namedArgs.get("ms"); |
24 | | - PiccodeValue.verifyType(scope.caller, ms, Type.NUMBER); |
25 | | - var path = namedArgs.get("path"); |
26 | | - return new PiccodeNumber("" + path); |
| 32 | + |
| 33 | + NativeFunctionFactory.create("filenew", List.of("path"), (args, namedArgs, frame) -> { |
| 34 | + var ctx = frame == null |
| 35 | + ? Context.top |
| 36 | + : Context.getContextAt(frame); |
| 37 | + |
| 38 | + var scope = ctx.getTopFrame(); |
| 39 | + var _path = namedArgs.get("path"); |
| 40 | + PiccodeValue.verifyType(scope.caller, _path, Type.STRING); |
| 41 | + |
| 42 | + var path = _path.raw().toString(); |
| 43 | + var fp = new File(path); |
| 44 | + var id = Context.allocate(fp); |
| 45 | + |
| 46 | + return makeFile(fp); |
27 | 47 | }, null); |
28 | | - |
29 | | - |
| 48 | + |
30 | 49 | } |
31 | 50 | } |
0 commit comments