Skip to content

Commit b78b1ac

Browse files
duonglaiquangrbri
authored andcommitted
FunctionWrapper: implement SymbolScriptable interface
1 parent 4d43a84 commit b78b1ac

File tree

2 files changed

+151
-1
lines changed

2 files changed

+151
-1
lines changed

src/main/java/org/htmlunit/javascript/FunctionWrapper.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919
import org.htmlunit.corejs.javascript.Context;
2020
import org.htmlunit.corejs.javascript.Function;
2121
import org.htmlunit.corejs.javascript.Scriptable;
22+
import org.htmlunit.corejs.javascript.Symbol;
23+
import org.htmlunit.corejs.javascript.SymbolScriptable;
2224

2325
/**
2426
* Wrapper for a {@link Function} delegating all calls to the wrapped instance.
2527
*
2628
* @author Marc Guillemot
2729
* @author Ahmed Ashour
2830
* @author Ronald Brill
31+
* @author Lai Quang Duong
2932
*/
30-
public class FunctionWrapper implements Function, Serializable {
33+
public class FunctionWrapper implements Function, SymbolScriptable, Serializable {
3134
private final Function wrapped_;
3235

3336
/**
@@ -78,6 +81,14 @@ public Object get(final int index, final Scriptable start) {
7881
return wrapped_.get(index, start);
7982
}
8083

84+
/**
85+
* {@inheritDoc}
86+
*/
87+
@Override
88+
public Object get(final Symbol key, final Scriptable start) {
89+
return ((SymbolScriptable) wrapped_).get(key, start);
90+
}
91+
8192
/**
8293
* {@inheritDoc}
8394
*/
@@ -94,6 +105,14 @@ public boolean has(final int index, final Scriptable start) {
94105
return wrapped_.has(index, start);
95106
}
96107

108+
/**
109+
* {@inheritDoc}
110+
*/
111+
@Override
112+
public boolean has(final Symbol key, final Scriptable start) {
113+
return ((SymbolScriptable) wrapped_).has(key, start);
114+
}
115+
97116
/**
98117
* {@inheritDoc}
99118
*/
@@ -110,6 +129,14 @@ public void put(final int index, final Scriptable start, final Object value) {
110129
wrapped_.put(index, wrapped_, value);
111130
}
112131

132+
/**
133+
* {@inheritDoc}
134+
*/
135+
@Override
136+
public void put(final Symbol key, final Scriptable start, final Object value) {
137+
((SymbolScriptable) wrapped_).put(key, wrapped_, value);
138+
}
139+
113140
/**
114141
* {@inheritDoc}
115142
*/
@@ -126,6 +153,14 @@ public void delete(final int index) {
126153
wrapped_.delete(index);
127154
}
128155

156+
/**
157+
* {@inheritDoc}
158+
*/
159+
@Override
160+
public void delete(final Symbol key) {
161+
((SymbolScriptable) wrapped_).delete(key);
162+
}
163+
129164
/**
130165
* {@inheritDoc}
131166
*/

src/test/java/org/htmlunit/javascript/FunctionsWrapperTest.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,119 @@ public void function_toString() throws Exception {
4646

4747
loadPageVerifyTitle2(html);
4848
}
49+
50+
/**
51+
* @throws Exception if the test fails
52+
*/
53+
@Test
54+
@Alerts("true")
55+
public void symbolHasInstance() throws Exception {
56+
final String html = DOCTYPE_HTML
57+
+ "<html><head>\n"
58+
+ "<script>\n"
59+
+ LOG_TITLE_FUNCTION
60+
+ "function test() {\n"
61+
+ " log(Function.prototype.toString instanceof Function);\n"
62+
+ "}\n"
63+
+ "</script></head><body onload='test()'>\n"
64+
+ "</body></html>";
65+
66+
loadPageVerifyTitle2(html);
67+
}
68+
69+
/**
70+
* @throws Exception if the test fails
71+
*/
72+
@Test
73+
@Alerts({"[object Function]", "[object Function]", "[object Function]"})
74+
public void symbolToStringTag() throws Exception {
75+
final String html = DOCTYPE_HTML
76+
+ "<html><head>\n"
77+
+ "<script>\n"
78+
+ LOG_TITLE_FUNCTION
79+
+ "function test() {\n"
80+
+ " var toString = Object.prototype.toString;\n"
81+
+ " log(toString.call(Function.prototype.toString));\n"
82+
+ " log(toString.call(Object.prototype.toString));\n"
83+
+ " log(toString.call(Array.prototype.toString));\n"
84+
+ "}\n"
85+
+ "</script></head><body onload='test()'>\n"
86+
+ "</body></html>";
87+
88+
loadPageVerifyTitle2(html);
89+
}
90+
91+
/**
92+
* @throws Exception if the test fails
93+
*/
94+
@Test
95+
@Alerts({"undefined", "undefined", "false", "false"})
96+
public void symbolPropertyAccess() throws Exception {
97+
final String html = DOCTYPE_HTML
98+
+ "<html><head>\n"
99+
+ "<script>\n"
100+
+ LOG_TITLE_FUNCTION
101+
+ "function test() {\n"
102+
+ " var fn = Function.prototype.toString;\n"
103+
+ " log(fn[Symbol.toStringTag]);\n"
104+
+ " log(fn[Symbol.toPrimitive]);\n"
105+
+ " log(Symbol.toStringTag in fn);\n"
106+
+ " log(Symbol.toPrimitive in fn);\n"
107+
+ "}\n"
108+
+ "</script></head><body onload='test()'>\n"
109+
+ "</body></html>";
110+
111+
loadPageVerifyTitle2(html);
112+
}
113+
114+
/**
115+
* @throws Exception if the test fails
116+
*/
117+
@Test
118+
@Alerts({"undefined", "hello", "true"})
119+
public void symbolPropertyWriteRead() throws Exception {
120+
final String html = DOCTYPE_HTML
121+
+ "<html><head>\n"
122+
+ "<script>\n"
123+
+ LOG_TITLE_FUNCTION
124+
+ "function test() {\n"
125+
+ " var fn = Function.prototype.toString;\n"
126+
+ " var sym = Symbol('test');\n"
127+
+ " log(fn[sym]);\n"
128+
+ " fn[sym] = 'hello';\n"
129+
+ " log(fn[sym]);\n"
130+
+ " log(sym in fn);\n"
131+
+ "}\n"
132+
+ "</script></head><body onload='test()'>\n"
133+
+ "</body></html>";
134+
135+
loadPageVerifyTitle2(html);
136+
}
137+
138+
/**
139+
* @throws Exception if the test fails
140+
*/
141+
@Test
142+
@Alerts({"hello", "true", "undefined", "false"})
143+
public void symbolPropertyDelete() throws Exception {
144+
final String html = DOCTYPE_HTML
145+
+ "<html><head>\n"
146+
+ "<script>\n"
147+
+ LOG_TITLE_FUNCTION
148+
+ "function test() {\n"
149+
+ " var fn = Function.prototype.toString;\n"
150+
+ " var sym = Symbol('del');\n"
151+
+ " fn[sym] = 'hello';\n"
152+
+ " log(fn[sym]);\n"
153+
+ " log(sym in fn);\n"
154+
+ " delete fn[sym];\n"
155+
+ " log(fn[sym]);\n"
156+
+ " log(sym in fn);\n"
157+
+ "}\n"
158+
+ "</script></head><body onload='test()'>\n"
159+
+ "</body></html>";
160+
161+
loadPageVerifyTitle2(html);
162+
}
163+
49164
}

0 commit comments

Comments
 (0)