Permalink
Browse files

implementing naïve eval();

  • Loading branch information...
1 parent f0e6de9 commit 09dd4169ca20e36381cbd0adaa36255b2f33b35f @qmx qmx committed May 3, 2012
View
2 src/main/java/org/dynjs/runtime/DynThreadContext.java
@@ -18,6 +18,7 @@
import me.qmx.jitescript.CodeBlock;
import org.dynjs.api.Function;
import org.dynjs.api.Scope;
+import org.dynjs.runtime.builtins.Eval;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@@ -34,6 +35,7 @@ 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<>();
View
37 src/main/java/org/dynjs/runtime/builtins/Eval.java
@@ -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.
+ }
+}
View
5 src/test/java/org/dynjs/runtime/DynJSTest.java
@@ -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 09dd416

Please sign in to comment.