annotate src/luan/Luan.java @ 1464:465b4a0dae4a

empty list vs map
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 03 Apr 2020 10:04:52 -0600
parents 219f2b937f2b
children e1a13e707bf3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
1 package luan;
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2
1454
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
3 import java.lang.reflect.Array;
1424
9ab267b9427c better load_file()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
4 import java.io.IOException;
318
4fe6c9fed486 add html processing
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 296
diff changeset
5 import java.util.List;
1113
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
6 import java.util.ArrayList;
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
7 import java.util.Map;
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
8 import java.util.HashMap;
1113
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
9 import java.util.LinkedHashMap;
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
10 import java.util.Iterator;
1113
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
11 import java.util.Arrays;
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
12 import java.util.Set;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
13 import goodjava.logging.Logger;
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1401
diff changeset
14 import goodjava.logging.LoggerFactory;
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
15 import luan.modules.JavaLuan;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
16 import luan.modules.PackageLuan;
1424
9ab267b9427c better load_file()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
17 import luan.modules.IoLuan;
1393
cc0dbca576dc better logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1337
diff changeset
18 import luan.modules.logging.LuanLogger;
694
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
19 import luan.impl.LuanCompiler;
134
b585d0915621 remove CmdLine.java and move code to Luan.java
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 113
diff changeset
20
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
21
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
22 public final class Luan implements LuanCloneable {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
23 private static final Logger logger = LoggerFactory.getLogger(Luan.class);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
24
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
25 private final List<LuanClosure> stack = new ArrayList<LuanClosure>();
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
26 private Map registry;
1335
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1334
diff changeset
27 private boolean isLocked = false;
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
28
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
29 public Luan() {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
30 registry = new HashMap();
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
31 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
32
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
33 private Luan(Luan luan) {}
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
34
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
35 @Override public Luan shallowClone() {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
36 return new Luan(this);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
37 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
38
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
39 @Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
40 Luan clone = (Luan)dc;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
41 clone.registry = cloner.clone(registry);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
42 if( cloner.type == LuanCloner.Type.INCREMENTAL )
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
43 isLocked = true;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
44 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
45
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
46 public LuanClosure peek() {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
47 return peek(1);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
48 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
49
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
50 public LuanClosure peek(int i) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
51 int n = stack.size();
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
52 return n < i ? null : stack.get(n-i);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
53 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
54
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
55 void push(LuanClosure closure) {
1335
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1334
diff changeset
56 if( isLocked )
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1334
diff changeset
57 throw new RuntimeException(this+" is locked "+closure);
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
58 stack.add(closure);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
59 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
60
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
61 void pop() {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
62 stack.remove(stack.size()-1);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
63 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
64
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
65 public Map registry() {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
66 return registry;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
67 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
68
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
69 public Object eval(String cmd,Object... args) throws LuanException {
1434
56fb5cd8228d cache compiled code in temp files
Franklin Schmidt <fschmidt@gmail.com>
parents: 1432
diff changeset
70 return load(cmd,"eval",false).call(args);
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
71 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
72
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
73 public Object require(String modName) throws LuanException {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
74 return PackageLuan.require(this,modName);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
75 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
76
1334
c88b486a9511 make some Luan methods static
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
77 public static String luanToString(Object obj) throws LuanException {
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
78 if( obj instanceof LuanTable ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
79 LuanTable tbl = (LuanTable)obj;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
80 return tbl.toStringLuan();
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
81 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
82 if( obj == null )
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
83 return "nil";
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
84 if( obj instanceof Number )
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
85 return Luan.toString((Number)obj);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
86 if( obj instanceof byte[] )
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
87 return "binary: " + Integer.toHexString(obj.hashCode());
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
88 return obj.toString();
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
89 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
90
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
91 public Object index(Object obj,Object key) throws LuanException {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
92 if( obj instanceof LuanTable ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
93 LuanTable tbl = (LuanTable)obj;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
94 return tbl.get(key);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
95 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
96 if( obj != null && peek().javaOk )
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
97 return JavaLuan.__index(this,obj,key);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
98 throw new LuanException("attempt to index a " + Luan.type(obj) + " value" );
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
99 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
100
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
101
1335
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1334
diff changeset
102 public static boolean isLessThan(Object o1,Object o2) throws LuanException {
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
103 if( o1 instanceof Number && o2 instanceof Number ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
104 Number n1 = (Number)o1;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
105 Number n2 = (Number)o2;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
106 return n1.doubleValue() < n2.doubleValue();
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
107 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
108 if( o1 instanceof String && o2 instanceof String ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
109 String s1 = (String)o1;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
110 String s2 = (String)o2;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
111 return s1.compareTo(s2) < 0;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
112 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
113 LuanFunction fn = getBinHandler("__lt",o1,o2);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
114 if( fn != null )
1335
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1334
diff changeset
115 return Luan.checkBoolean( Luan.first(fn.call(o1,o2)) );
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
116 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
117 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
118
1334
c88b486a9511 make some Luan methods static
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
119 public static LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
120 if( o1 instanceof LuanTable ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
121 LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
122 if( f1 != null )
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
123 return f1;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
124 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
125 return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
126 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
127
1334
c88b486a9511 make some Luan methods static
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
128 public static LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
129 Object f = t.getHandler(op);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
130 if( f == null )
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
131 return null;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
132 return Luan.checkFunction(f);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
133 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
134
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
135 public LuanTable toTable(Object obj) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
136 if( obj == null )
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
137 return null;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
138 if( obj instanceof LuanTable )
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
139 return (LuanTable)obj;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
140 if( obj instanceof List ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
141 return new LuanTable(this,(List)obj);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
142 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
143 if( obj instanceof Map ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
144 return new LuanTable(this,(Map)obj);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
145 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
146 if( obj instanceof Set ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
147 return new LuanTable(this,(Set)obj);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
148 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
149 Class cls = obj.getClass();
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
150 if( cls.isArray() ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
151 if( cls.getComponentType().isPrimitive() ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
152 int len = Array.getLength(obj);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
153 List list = new ArrayList();
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
154 for( int i=0; i<len; i++ ) {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
155 list.add(Array.get(obj,i));
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
156 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
157 return new LuanTable(this,list);
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
158 } else {
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
159 Object[] a = (Object[])obj;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
160 return new LuanTable(this,Arrays.asList(a));
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
161 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
162 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
163 return null;
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
164 }
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
165
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
166
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
167
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
168 // static
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
169
481
5d4a78c93383 luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents: 446
diff changeset
170 public static void main(String[] args) throws LuanException {
1454
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
171 Luan luan = new Luan();
1424
9ab267b9427c better load_file()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
172 LuanFunction fn = loadClasspath(luan,"luan/cmd_line.luan");
1335
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1334
diff changeset
173 fn.call((Object[])args);
134
b585d0915621 remove CmdLine.java and move code to Luan.java
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 113
diff changeset
174 }
1454
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
175
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
176 public static LuanFunction loadClasspath(Luan luan,String classpath)
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
177 throws LuanException
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
178 {
1424
9ab267b9427c better load_file()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
179 try {
1454
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
180 String src = IoLuan.classpath(luan,classpath).read_text();
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
181 return luan.load(src,"classpath:"+classpath,true);
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
182 } catch(IOException e) {
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
183 throw new RuntimeException(e);
1424
9ab267b9427c better load_file()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
184 }
9ab267b9427c better load_file()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
185 }
1454
219f2b937f2b remove log4j
Franklin Schmidt <fschmidt@gmail.com>
parents: 1442
diff changeset
186
111
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
187 public static Object first(Object obj) {
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
188 if( !(obj instanceof Object[]) )
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
189 return obj;
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
190 Object[] a = (Object[])obj;
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
191 return a.length==0 ? null : a[0];
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
192 }
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
193
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
194 public static Object[] array(Object obj) {
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
195 return obj instanceof Object[] ? (Object[])obj : new Object[]{obj};
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
196 }
2428ecfed375 change LuanFunction.call() from returning Object[] to returning Object, to reduce garbage collection
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 105
diff changeset
197
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
198 public static String type(Object obj) {
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
199 if( obj == null )
105
a9560839104d minor - change type names to lower case like Lua
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 104
diff changeset
200 return "nil";
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
201 if( obj instanceof String )
105
a9560839104d minor - change type names to lower case like Lua
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 104
diff changeset
202 return "string";
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
203 if( obj instanceof Boolean )
105
a9560839104d minor - change type names to lower case like Lua
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 104
diff changeset
204 return "boolean";
46
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
205 if( obj instanceof Number )
105
a9560839104d minor - change type names to lower case like Lua
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 104
diff changeset
206 return "number";
85
b2551f00bc51 fix type()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 66
diff changeset
207 if( obj instanceof LuanTable )
105
a9560839104d minor - change type names to lower case like Lua
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 104
diff changeset
208 return "table";
85
b2551f00bc51 fix type()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 66
diff changeset
209 if( obj instanceof LuanFunction )
105
a9560839104d minor - change type names to lower case like Lua
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 104
diff changeset
210 return "function";
113
8c706d6eb5dc add binary type
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 111
diff changeset
211 if( obj instanceof byte[] )
8c706d6eb5dc add binary type
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 111
diff changeset
212 return "binary";
513
0dfc01d8d42d rename type "userdata" to "java"
Franklin Schmidt <fschmidt@gmail.com>
parents: 481
diff changeset
213 return "java";
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
214 }
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
215
46
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
216 public static String toString(Number n) {
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
217 if( n instanceof Integer )
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
218 return n.toString();
255
e5ad26746c94 improve toString(Number)
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 226
diff changeset
219 int i = n.intValue();
e5ad26746c94 improve toString(Number)
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 226
diff changeset
220 if( i == n.doubleValue() )
e5ad26746c94 improve toString(Number)
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 226
diff changeset
221 return Integer.toString(i);
46
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
222 String s = n.toString();
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
223 int iE = s.indexOf('E');
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
224 String ending = null;
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
225 if( iE != -1 ) {
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
226 ending = s.substring(iE);
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
227 s = s.substring(0,iE);
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
228 }
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
229 if( s.endsWith(".0") )
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
230 s = s.substring(0,s.length()-2);
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
231 if( ending != null )
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
232 s += ending;
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
233 return s;
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
234 }
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
235
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
236 public static Integer asInteger(Object obj) {
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
237 if( obj instanceof Integer )
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
238 return (Integer)obj;
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
239 if( !(obj instanceof Number) )
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
240 return null;
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
241 Number n = (Number)obj;
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
242 int i = n.intValue();
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
243 return i==n.doubleValue() ? Integer.valueOf(i) : null;
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
244 }
66
f7e17cfb35f9 minor toString() improvement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
245
1442
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
246 public static Long asLong(Object obj) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
247 if( obj instanceof Long )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
248 return (Long)obj;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
249 if( !(obj instanceof Number) )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
250 return null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
251 Number n = (Number)obj;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
252 long i = n.longValue();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
253 return i==n.doubleValue() ? Long.valueOf(i) : null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
254 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
255
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
256 public static Float asFloat(Object obj) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
257 if( obj instanceof Float )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
258 return (Float)obj;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
259 if( !(obj instanceof Number) )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
260 return null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
261 Number n = (Number)obj;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
262 float i = n.floatValue();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
263 return i==n.doubleValue() ? Float.valueOf(i) : null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
264 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
265
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
266 public static Double asDouble(Object obj) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
267 if( obj instanceof Double )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
268 return (Double)obj;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
269 if( !(obj instanceof Number) )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
270 return null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
271 Number n = (Number)obj;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
272 double i = n.doubleValue();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
273 return Double.valueOf(i);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
274 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1434
diff changeset
275
88
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
276 public static String stringEncode(String s) {
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
277 s = s.replace("\\","\\\\");
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
278 s = s.replace("\u0007","\\a");
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
279 s = s.replace("\b","\\b");
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
280 s = s.replace("\f","\\f");
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
281 s = s.replace("\n","\\n");
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
282 s = s.replace("\r","\\r");
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
283 s = s.replace("\t","\\t");
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
284 s = s.replace("\u000b","\\v");
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
285 s = s.replace("\"","\\\"");
420
e9d4d5854e54 add String.encode
Franklin Schmidt <fschmidt@gmail.com>
parents: 419
diff changeset
286 s = s.replace("\'","\\'");
88
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
287 return s;
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
288 }
6ca02b188dba add LuanBit to clean up code;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 85
diff changeset
289
647
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
290
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
291 public static Boolean checkBoolean(Object obj) throws LuanException {
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
292 if( obj instanceof Boolean )
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
293 return (Boolean)obj;
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
294 throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a boolean" );
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
295 }
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
296
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
297 public static String checkString(Object obj) throws LuanException {
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
298 if( obj instanceof String )
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
299 return (String)obj;
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
300 throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a string" );
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
301 }
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
302
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
303 public static LuanFunction checkFunction(Object obj) throws LuanException {
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
304 if( obj instanceof LuanFunction )
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
305 return (LuanFunction)obj;
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
306 throw new LuanException("attempt to call a " + Luan.type(obj) + " value" );
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
307 }
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
308
1434
56fb5cd8228d cache compiled code in temp files
Franklin Schmidt <fschmidt@gmail.com>
parents: 1432
diff changeset
309 public LuanFunction load(String text,String sourceName,boolean persist,LuanTable env)
694
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
310 throws LuanException
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
311 {
1434
56fb5cd8228d cache compiled code in temp files
Franklin Schmidt <fschmidt@gmail.com>
parents: 1432
diff changeset
312 return LuanCompiler.compile(this,text,sourceName,persist,env);
694
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
313 }
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
314
1434
56fb5cd8228d cache compiled code in temp files
Franklin Schmidt <fschmidt@gmail.com>
parents: 1432
diff changeset
315 public LuanFunction load(String text,String sourceName,boolean persist)
694
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
316 throws LuanException
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
317 {
1434
56fb5cd8228d cache compiled code in temp files
Franklin Schmidt <fschmidt@gmail.com>
parents: 1432
diff changeset
318 return load(text,sourceName,persist,null);
694
b620b8e1010f remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents: 693
diff changeset
319 }
647
8e8c30b72e9b move methods from LuanState to Luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 574
diff changeset
320
1113
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
321 public static Object toJava(Object obj) throws LuanException {
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
322 if( !(obj instanceof LuanTable) )
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
323 return obj;
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
324 LuanTable tbl = (LuanTable)obj;
1464
465b4a0dae4a empty list vs map
Franklin Schmidt <fschmidt@gmail.com>
parents: 1454
diff changeset
325 if( !tbl.isMap() ) {
1113
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
326 List list = new ArrayList();
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
327 for( Object el : tbl.asList() ) {
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
328 list.add( toJava(el) );
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
329 }
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
330 return list;
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
331 } else {
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
332 Map map = new LinkedHashMap();
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
333 Iterator<Map.Entry> iter = tbl.rawIterator();
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
334 while( iter.hasNext() ) {
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
335 Map.Entry entry = iter.next();
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
336 map.put( toJava(entry.getKey()), toJava(entry.getValue()) );
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
337 }
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
338 return map;
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
339 }
22652f4020fb add JsonToString
Franklin Schmidt <fschmidt@gmail.com>
parents: 775
diff changeset
340 }
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
341
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
342
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
343 // security
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
344
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
345 public interface Security {
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
346 public void check(Luan luan,LuanClosure closure,String op,Object... args) throws LuanException;
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
347 }
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
348
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
349 private static String SECURITY_KEY = "Luan.Security";
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
350
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
351 public static void checkSecurity(Luan luan,String op,Object... args) throws LuanException {
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
352 check(luan,1,op,args);
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
353 }
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
354
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
355 public static void checkCallerSecurity(Luan luan,String op,Object... args) throws LuanException {
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
356 check(luan,2,op,args);
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
357 }
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
358
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
359 private static void check(Luan luan,int i,String op,Object... args) throws LuanException {
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
360 Security s = (Security)luan.registry().get(SECURITY_KEY);
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
361 if( s!=null )
1432
Franklin Schmidt <fschmidt@gmail.com>
parents: 1424
diff changeset
362 s.check(luan,luan.peek(i),op,args);
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
363 }
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
364
1333
25746915a241 merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents: 1330
diff changeset
365 public static Security setSecurity(Luan luan,Security s) {
1330
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
366 return (Security)luan.registry().put(SECURITY_KEY,s);
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
367 }
f41919741100 fix security
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
368
1
2df768b926aa start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
369 }