Skip to content

Commit

Permalink
implementing dyn:setProp:<x>
Browse files Browse the repository at this point in the history
  • Loading branch information
qmx committed Jan 24, 2012
1 parent a45e609 commit 0a49439
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
12 changes: 11 additions & 1 deletion dynjs/src/main/java/org/dynjs/runtime/linker/DynJSLinker.java
Expand Up @@ -76,7 +76,17 @@ public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest, LinkerSer
return new GuardedInvocation(RESOLVE, Guards.isInstance(Scope.class, RESOLVE.type()));
}
} else if ("setProp".equals(callSiteDescriptor.getNameToken(1))) {
return new GuardedInvocation(DEFINE, Guards.isInstance(Scope.class, DEFINE.type()));
if (hasConstantCall(callSiteDescriptor)) {
final MethodHandle handle = Binder
.from(void.class, Object.class, Object.class)
.convert(DEFINE.type())
.insert(1, callSiteDescriptor.getNameToken(2))
.invoke(DEFINE);
return new GuardedInvocation(handle,
Guards.isInstance(Scope.class, handle.type()));
} else {
return new GuardedInvocation(DEFINE, Guards.isInstance(Scope.class, DEFINE.type()));
}
}
} else if (callSiteDescriptor.getNameTokenCount() >= 3 && callSiteDescriptor.getNameToken(0).equals("dynjs")) {
String action = callSiteDescriptor.getNameToken(2);
Expand Down
24 changes: 24 additions & 0 deletions dynjs/src/test/java/org/dynjs/runtime/DynalinkTest.java
Expand Up @@ -98,4 +98,28 @@ public void testSetPropNonConstantName() {
assertThat(((Scope) x).resolve("o")).isNotNull().isEqualTo("any");
}

@Test
public void testSetPropConstantName() {
dynJS.eval(context, "var x = {w:function(){return 1;}};");
final Object x = context.getScope().resolve("x");
final CodeBlock codeBlock = CodeBlock.newCodeBlock()
.aload(0)
.ldc("x")
.invokeinterface(DynJSCompiler.Types.Scope, "resolve", sig(Object.class, String.class))
.ldc("any")
.invokedynamic("dyn:setProp:o", sig(void.class, Object.class, Object.class), RT.BOOTSTRAP, RT.BOOTSTRAP_ARGS)
.aconst_null()
.areturn();
final Function fn = dynJS.compile(codeBlock, new String[]{});
fn.define("x", x);
final Object call = fn.call(context, new Object[]{});

assertThat(x)
.isNotNull()
.isInstanceOf(Scope.class);

assertThat(((Scope) x).resolve("o")).isNotNull().isEqualTo("any");
}


}

0 comments on commit 0a49439

Please sign in to comment.