Permalink
Browse files

Merge branch 'simple_eval'

  • Loading branch information...
qmx committed May 3, 2012
2 parents 793dac5 + 09dd416 commit 3f970be2056de16cab6cac276159fb0617af5333
@@ -18,11 +18,9 @@
import me.qmx.jitescript.CodeBlock;
import org.dynjs.api.Function;
import org.dynjs.api.Scope;
+import org.dynjs.runtime.builtins.Eval;
-import java.util.Deque;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.Map;
+import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public class DynThreadContext {
@@ -35,13 +33,24 @@ public String toString() {
};
public static final Object NULL = new Object();
+
+ private static final Map<String, Object> BUILTINS = new LinkedHashMap<String, Object>() {{
+ put("eval", new Eval());
+ }};
+
private ThreadLocal<DynJS> runtime = new ThreadLocal<>();
private AtomicInteger storageCounter = new AtomicInteger();
private Map<Integer, CodeBlock> storage = new HashMap<>();
private Scope scope = new DynObject();
private Deque<Function> callStack = new LinkedList<>();
private DynamicClassLoader classLoader;
+ public DynThreadContext() {
+ for (Map.Entry<String, Object> builin : BUILTINS.entrySet()) {
+ scope.define(builin.getKey(), builin.getValue());
+ }
+ }
+
public DynJS getRuntime() {
return this.runtime.get();
}
@@ -102,4 +111,5 @@ public void setClassLoader(DynamicClassLoader classLoader) {
public DynamicClassLoader getClassLoader() {
return classLoader;
}
+
}
@@ -0,0 +1,37 @@
+package org.dynjs.runtime.builtins;
+
+import org.dynjs.api.Function;
+import org.dynjs.api.Scope;
+import org.dynjs.runtime.DynJS;
+import org.dynjs.runtime.DynThreadContext;
+
+public class Eval implements Function {
+ @Override
+ public void setContext(DynThreadContext context) {
+
+ }
+
+ @Override
+ public Object call(DynThreadContext context, Object[] arguments) {
+ if (arguments.length == 1 && arguments[0] instanceof String) {
+ DynJS runtime = context.getRuntime();
+ runtime.eval(context, (String) arguments[0]);
+ }
+ return DynThreadContext.UNDEFINED;
+ }
+
+ @Override
+ public Scope getEnclosingScope() {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ public Object resolve(String name) {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ @Override
+ public void define(String property, Object value) {
+ //To change body of implemented methods use File | Settings | File Templates.
+ }
+}
@@ -248,6 +248,11 @@ public void testBuiltinLoading() {
check("var result = sample(true);");
}
+ @Test
+ public void testEval(){
+ check("eval('var result = true');");
+ }
+
private void check(String scriptlet) {
check(scriptlet, true);
}

0 comments on commit 3f970be

Please sign in to comment.