Using Object.defineProperty to hide attributes from list of enumerable properties
1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Mon Jun 09 19:32:49 2014 +0200
1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sun Jun 22 00:09:56 2014 +0200
1.3 @@ -260,15 +260,18 @@
1.4 append("\n c.constructor = CLS;");
1.5 append("\n function fillInstOf(x) {");
1.6 String instOfName = "$instOf_" + className;
1.7 - append("\n x['").append(instOfName).append("'] = true;");
1.8 + append("\n Object.defineProperty(x, '").append(instOfName).append("', { value : true });");
1.9 for (String superInterface : jc.getSuperInterfaces()) {
1.10 String intrfc = superInterface.replace('/', '_');
1.11 append("\n vm.").append(intrfc).append("(false)['fillInstOf'](x);");
1.12 requireReference(superInterface);
1.13 }
1.14 append("\n }");
1.15 - append("\n c['fillInstOf'] = fillInstOf;");
1.16 - append("\n fillInstOf(c);");
1.17 + append("\n try {");
1.18 + append("\n Object.defineProperty(c, 'fillInstOf', { value: fillInstOf });");
1.19 + append("\n fillInstOf(c);");
1.20 + append("\n } catch (ignore) {");
1.21 + append("\n }");
1.22 // obfuscationDelegate.exportJSProperty(this, "c", instOfName);
1.23 append("\n CLS.$class = 'temp';");
1.24 append("\n CLS.$class = ");
2.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Instance.java Mon Jun 09 19:32:49 2014 +0200
2.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Instance.java Sun Jun 22 00:09:56 2014 +0200
2.3 @@ -135,6 +135,13 @@
2.4 return jsgetbytes(new Instance());
2.5 }
2.6
2.7 + public static int noInstOfExposed() {
2.8 + return countInstOf(10);
2.9 + }
2.10 +
2.11 + @JavaScriptBody(args = "o", body = "var i = 0; for (p in o) { if (p.toString().indexOf('instOf') >= 0) i++; } return i;")
2.12 + private static native int countInstOf(Object o);
2.13 +
2.14 @JavaScriptBody(args = { "instance" }, body = "return instance.getByte__B();")
2.15 private static native int jsgetbytes(Instance instance);
2.16
3.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java Mon Jun 09 19:32:49 2014 +0200
3.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java Sun Jun 22 00:09:56 2014 +0200
3.3 @@ -47,6 +47,13 @@
3.4 Double.valueOf(31)
3.5 );
3.6 }
3.7 + @Test public void noInstOfExposed() throws Exception {
3.8 + assertExec(
3.9 + "No instOf properties found",
3.10 + Instance.class, "noInstOfExposed__I",
3.11 + Double.valueOf(0)
3.12 + );
3.13 + }
3.14 @Test public void verifyMagicOne() throws Exception {
3.15 assertExec(
3.16 "Should be three and something",