Fix constructor injection to use new constructor type, add test

This commit is contained in:
Daniel Silverstone 2015-08-09 18:54:37 +01:00
parent 3d7de6cf8d
commit 0df3439eb2
5 changed files with 69 additions and 3 deletions

View File

@ -247,7 +247,27 @@ dukky_push_node(duk_context *ctx, struct dom_node *node)
return dukky_push_node_stacked(ctx);
}
static duk_ret_t
dukky_bad_constructor(duk_context *ctx)
{
duk_error(ctx, DUK_ERR_ERROR, "Bad constructor");
return 0;
}
void
dukky_inject_not_ctr(duk_context *ctx, int idx, const char *name)
{
/* ... p[idx] ... proto */
duk_push_c_function(ctx, dukky_bad_constructor, 0);
/* ... p[idx] ... proto cons */
duk_insert(ctx, -2);
/* ... p[idx] ... cons proto */
duk_put_prop_string(ctx, -2, "prototype");
/* ... p[idx] ... cons[proto] */
duk_put_prop_string(ctx, idx, name);
/* ... p ... */
return;
}
/**************************************** js.h ******************************/
struct jscontext {

View File

@ -27,6 +27,6 @@
duk_ret_t dukky_create_object(duk_context *ctx, const char *name, int args);
duk_bool_t dukky_push_node_stacked(duk_context *ctx);
duk_bool_t dukky_push_node(duk_context *ctx, struct dom_node *node);
void dukky_inject_not_ctr(duk_context *ctx, int idx, const char *name);
#endif

View File

@ -31,9 +31,14 @@ init Window("struct browser_window *" win, "struct html_content *" htmlc)
prototype Window()
%{
#define EXPOSE(v) \
duk_get_global_string(ctx, #v); \
duk_put_prop_string(ctx, 0, #v)
/* steal undefined */
duk_get_global_string(ctx, "undefined");
duk_put_prop_string(ctx, 0, "undefined");
EXPOSE(undefined);
EXPOSE(eval);
EXPOSE(Object);
#undef EXPOSE
%}
getter Window::document()

View File

@ -69,6 +69,7 @@
<ul>
<li><a href="assorted-log-doc-write.html">console.log and document.write</a></li>
<li><a href="wikipedia-lcm.html">Example from wikipedia</a></li>
<li><a href="verify-instanceofness.html">Check instanceof behaviour</a></li>
</ul>
</body>

View File

@ -0,0 +1,40 @@
<html>
<head><title>Verify instanceof checking</title></head>
<body>
<h1>Check instanceof behaviour</h1>
<table cellpadding=2 border=1>
<tr><th>A</th><th>instanceof</th><th>B</th><th>?</th><th>Correct?</th></tr>
<script>
var checks = [
[ "window", "Window", true ],
[ "document", "HTMLDocument", true ],
[ "document.head", "Node", true ],
[ "document.getElementsByTagName(\"body\")", "HTMLCollection", true ],
[ "document.body", "Window", false ],
[ "EventListener", "Object", undefined ],
];
for (var _check in checks) {
var check = checks[_check];
document.write("<tr>");
document.write("<td>" + check[0] + "</td><td>instanceof</td><td>" + check[1] + "</td>");
try {
var A = eval(check[0]);
var B = eval(check[1]);
var C = check[2];
var V = A instanceof B;
var OK = V == C;
document.write("<td>" + V + "</td><td>" + (OK ? "YES" : "<b style=\"color: red\">NO</b>") + "</td>");
} catch (e) {
if (check[2] == undefined) {
document.write("<td>" + e + "</td><td>YES</td>");
} else {
document.write("<td colspan=2><b style=\"color: red\">" + e + "</b></td>");
}
}
document.write("</tr>");
}
</script>
</table>
</body>
</html>