Skip to content

Commit 1fb3c79

Browse files
committed
PiccodeClosure: Allow exporting object values
1 parent 0d1aec7 commit 1fb3c79

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/main/java/org/piccode/rt/PiccodeClosure.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.piccode.rt;
22

3+
import com.github.tomaslanger.chalk.Chalk;
34
import java.util.HashMap;
45
import java.util.List;
56
import java.util.Map;
@@ -115,7 +116,18 @@ public PiccodeValue evaluateIfReady() {
115116
param.name,
116117
param.def_val != null ? param.def_val.execute(frame) : null
117118
);
118-
ctx.putLocal(param.name, val);
119+
120+
if (param.export && !(val instanceof PiccodeObject)) {
121+
throw new PiccodeException(param.file, param.line , param.column, "Cannot export fields of a value that is not an object. Found type " + Chalk.on(val.type()).red());
122+
} else if (param.export && val instanceof PiccodeObject obj) {
123+
for (var kv : obj.obj.entrySet()) {
124+
var name = kv.getKey();
125+
var value = kv.getValue();
126+
ctx.putLocal(name, value);
127+
}
128+
} else {
129+
ctx.putLocal(param.name, val);
130+
}
119131
}
120132

121133
try {

0 commit comments

Comments
 (0)