Skip to content

Commit

Permalink
implementing new
Browse files Browse the repository at this point in the history
  • Loading branch information
qmx committed May 6, 2012
1 parent e939f51 commit 4581f1e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/dynjs/compiler/DynJSCompiler.java
Expand Up @@ -174,7 +174,7 @@ public static interface Helper {
}

public static class InternalDynObject extends DynObject {
public InternalDynObject(Object prototype, Function function) {
public InternalDynObject(Object prototype, Object function) {
setProperty("prototype", prototype);
setProperty("call", function);
setProperty("construct", function);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/dynjs/parser/Executor.java
Expand Up @@ -17,6 +17,7 @@

import me.qmx.jitescript.CodeBlock;
import org.antlr.runtime.tree.CommonTree;
import org.dynjs.compiler.DynJSCompiler;
import org.dynjs.exception.DynJSException;
import org.dynjs.parser.statement.*;
import org.dynjs.runtime.DynThreadContext;
Expand Down Expand Up @@ -316,8 +317,9 @@ public Statement executeNew(final Statement statement) {
@Override
public CodeBlock getCodeBlock() {
return CodeBlock.newCodeBlock()
.aload(DynJSCompiler.Arities.CONTEXT)
.append(statement.getCodeBlock())
.invokedynamic("newInstance", sig(Object.class, Object.class), RT.BOOTSTRAP, RT.BOOTSTRAP_ARGS);
.invokedynamic("new", sig(Object.class, DynThreadContext.class, Object.class), RT.BOOTSTRAP, RT.BOOTSTRAP_ARGS);
}
};
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/dynjs/runtime/RT.java
Expand Up @@ -16,11 +16,13 @@
package org.dynjs.runtime;

import org.dynjs.api.Function;
import org.dynjs.compiler.DynJSCompiler;
import org.dynjs.runtime.linker.DynJSBootstrapper;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Opcodes;

import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Map;
Expand All @@ -34,6 +36,15 @@ public class RT {
p(DynJSBootstrapper.class), "bootstrap", methodType(CallSite.class,
MethodHandles.Lookup.class, String.class, MethodType.class).toMethodDescriptorString());
public static final Object[] BOOTSTRAP_ARGS = new Object[0];
public static final MethodHandle CONSTRUCT;

static {
try {
CONSTRUCT = MethodHandles.lookup().findStatic(RT.class, "construct", methodType(Object.class, DynThreadContext.class, Object.class));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static DynFunction paramPopulator(DynFunction function, Object[] args) {
String[] parameters = function.getArguments();
Expand Down Expand Up @@ -83,4 +94,8 @@ public static String typeof(Object obj) {

return "undefined";
}

public static Object construct(DynThreadContext context, Object obj) {
return new DynJSCompiler.InternalDynObject(obj, null);
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/dynjs/runtime/linker/DynJSLinker.java
Expand Up @@ -84,6 +84,8 @@ public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerSer
.invokeVirtual(lookup(), "typeof");
return new GuardedInvocation(typeof, null);
}
} else if ("new".equals(callSiteDescriptor.getName())) {
return new GuardedInvocation(RT.CONSTRUCT, null);
} else if ("eq".equals(callSiteDescriptor.getName()) && argumentsAreNotStrings(linkRequest.getArguments())) {
targetHandle = lookup().findStatic(ObjectOperations.class, "eq", methodType);
} else if (isFromDynalink(callSiteDescriptor)) {
Expand Down

0 comments on commit 4581f1e

Please sign in to comment.