Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/main/java/org/htmlunit/javascript/FunctionWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import org.htmlunit.corejs.javascript.Context;
import org.htmlunit.corejs.javascript.Function;
import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.corejs.javascript.Symbol;
import org.htmlunit.corejs.javascript.SymbolScriptable;

/**
* Wrapper for a {@link Function} delegating all calls to the wrapped instance.
*
* @author Marc Guillemot
* @author Ahmed Ashour
* @author Ronald Brill
* @author Lai Quang Duong
*/
public class FunctionWrapper implements Function, Serializable {
public class FunctionWrapper implements Function, SymbolScriptable, Serializable {
private final Function wrapped_;

/**
Expand Down Expand Up @@ -78,6 +81,14 @@ public Object get(final int index, final Scriptable start) {
return wrapped_.get(index, start);
}

/**
* {@inheritDoc}
*/
@Override
public Object get(final Symbol key, final Scriptable start) {
return ((SymbolScriptable) wrapped_).get(key, start);
}

/**
* {@inheritDoc}
*/
Expand All @@ -94,6 +105,14 @@ public boolean has(final int index, final Scriptable start) {
return wrapped_.has(index, start);
}

/**
* {@inheritDoc}
*/
@Override
public boolean has(final Symbol key, final Scriptable start) {
return ((SymbolScriptable) wrapped_).has(key, start);
}

/**
* {@inheritDoc}
*/
Expand All @@ -110,6 +129,14 @@ public void put(final int index, final Scriptable start, final Object value) {
wrapped_.put(index, wrapped_, value);
}

/**
* {@inheritDoc}
*/
@Override
public void put(final Symbol key, final Scriptable start, final Object value) {
((SymbolScriptable) wrapped_).put(key, wrapped_, value);
}

/**
* {@inheritDoc}
*/
Expand All @@ -126,6 +153,14 @@ public void delete(final int index) {
wrapped_.delete(index);
}

/**
* {@inheritDoc}
*/
@Override
public void delete(final Symbol key) {
((SymbolScriptable) wrapped_).delete(key);
}

/**
* {@inheritDoc}
*/
Expand Down
115 changes: 115 additions & 0 deletions src/test/java/org/htmlunit/javascript/FunctionsWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,119 @@ public void function_toString() throws Exception {

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts("true")
public void symbolHasInstance() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ "function test() {\n"
+ " log(Function.prototype.toString instanceof Function);\n"
+ "}\n"
+ "</script></head><body onload='test()'>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts({"[object Function]", "[object Function]", "[object Function]"})
public void symbolToStringTag() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ "function test() {\n"
+ " var toString = Object.prototype.toString;\n"
+ " log(toString.call(Function.prototype.toString));\n"
+ " log(toString.call(Object.prototype.toString));\n"
+ " log(toString.call(Array.prototype.toString));\n"
+ "}\n"
+ "</script></head><body onload='test()'>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts({"undefined", "undefined", "false", "false"})
public void symbolPropertyAccess() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ "function test() {\n"
+ " var fn = Function.prototype.toString;\n"
+ " log(fn[Symbol.toStringTag]);\n"
+ " log(fn[Symbol.toPrimitive]);\n"
+ " log(Symbol.toStringTag in fn);\n"
+ " log(Symbol.toPrimitive in fn);\n"
+ "}\n"
+ "</script></head><body onload='test()'>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts({"undefined", "hello", "true"})
public void symbolPropertyWriteRead() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ "function test() {\n"
+ " var fn = Function.prototype.toString;\n"
+ " var sym = Symbol('test');\n"
+ " log(fn[sym]);\n"
+ " fn[sym] = 'hello';\n"
+ " log(fn[sym]);\n"
+ " log(sym in fn);\n"
+ "}\n"
+ "</script></head><body onload='test()'>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts({"hello", "true", "undefined", "false"})
public void symbolPropertyDelete() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ "function test() {\n"
+ " var fn = Function.prototype.toString;\n"
+ " var sym = Symbol('del');\n"
+ " fn[sym] = 'hello';\n"
+ " log(fn[sym]);\n"
+ " log(sym in fn);\n"
+ " delete fn[sym];\n"
+ " log(fn[sym]);\n"
+ " log(sym in fn);\n"
+ "}\n"
+ "</script></head><body onload='test()'>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

}
Loading