Skip to content

Commit

Permalink
setter function (from property descriptor) has to convert the args
Browse files Browse the repository at this point in the history
(see HtmlUnit#7) also
  • Loading branch information
rbri committed Feb 19, 2023
1 parent 1567f6c commit d22447c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/org/mozilla/javascript/MemberBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ public Object call(
Object setterThis;
Object[] args;
Object value =
originalArgs.length > 0 ? originalArgs[0] : Undefined.instance;
originalArgs.length > 0
? FunctionObject.convertArg(
cx,
thisObj,
originalArgs[0],
FunctionObject.getTypeTag(
nativeSetter.argTypes[0]))
: Undefined.instance;
if (nativeSetter.delegateTo == null) {
setterThis = thisObj;
args = new Object[] {value};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
import static org.mozilla.javascript.tests.Evaluator.eval;

import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSGetter;
import org.mozilla.javascript.annotations.JSSetter;

public class ObjectGetOwnPropertyDescriptorTest {

Expand Down Expand Up @@ -40,4 +45,48 @@ public void testContentsOfPropertyDescriptorShouldReflectAttributesOfProperty()
assertEquals(false, descriptor.get("writable"));
assertEquals(false, descriptor.get("configurable"));
}

@Test
public void callPropertyDescriptorSetterWithConsString() throws Exception {
try (Context cx = Context.enter()) {
Scriptable scope = cx.initStandardObjects();
ScriptableObject.defineClass(scope, AnnotatedHostObject.class);

final String script =
"var hostObj = new AnnotatedHostObject();\n"
+ "var valueProperty = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(hostObj), 'myProp');\n"
+ "var consString = 'a';\n"
+ "consString = consString + 'bc';\n"
+ "valueProperty.set.call(hostObj, consString);\n"
+ "var result = '' + valueProperty.get.call(hostObj);\n"
+ "result;\n";
String result = (String) cx.evaluateString(scope, script, "<testsrc>", 0, null);
assertEquals("abc", result);
}
}

public static class AnnotatedHostObject extends ScriptableObject {

String myProp;

public AnnotatedHostObject() {}

@Override
public String getClassName() {
return "AnnotatedHostObject";
}

@JSConstructor
public void jsConstructorMethod() {}

@JSGetter
public String getMyProp() {
return myProp;
}

@JSSetter
public void setMyProp(String prop) {
myProp = prop;
}
}
}

0 comments on commit d22447c

Please sign in to comment.