jaroslav@24: /** jaroslav@106: * Back 2 Browser Bytecode Translator jaroslav@106: * Copyright (C) 2012 Jaroslav Tulach jaroslav@24: * jaroslav@24: * This program is free software: you can redistribute it and/or modify jaroslav@24: * it under the terms of the GNU General Public License as published by jaroslav@24: * the Free Software Foundation, version 2 of the License. jaroslav@24: * jaroslav@24: * This program is distributed in the hope that it will be useful, jaroslav@24: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@24: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@24: * GNU General Public License for more details. jaroslav@24: * jaroslav@24: * You should have received a copy of the GNU General Public License jaroslav@24: * along with this program. Look for COPYING file in the top folder. jaroslav@24: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@8: */ jaroslav@22: package org.apidesign.vm4brwsr; jaroslav@8: jaroslav@226: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@226: jaroslav@8: /** jaroslav@8: * jaroslav@8: * @author Jaroslav Tulach jaroslav@8: */ jaroslav@8: public class Instance { jaroslav@137: private int in; jaroslav@8: protected short s; jaroslav@8: public double d; jaroslav@8: private float f; jaroslav@10: protected byte b = (byte)31; jaroslav@10: jaroslav@10: private Instance() { jaroslav@10: } jaroslav@8: jaroslav@8: public Instance(int i, double d) { jaroslav@137: this.in = i; jaroslav@8: this.d = d; jaroslav@8: } jaroslav@8: public byte getByte() { jaroslav@8: return b; jaroslav@8: } jaroslav@8: jaroslav@8: public void setByte(byte b) { jaroslav@8: this.b = b; jaroslav@8: } jaroslav@10: public static double defaultDblValue() { jaroslav@10: Instance create = new Instance(); jaroslav@10: return create.d; jaroslav@10: } jaroslav@8: jaroslav@10: public static byte assignedByteValue() { jaroslav@10: return new Instance().b; jaroslav@10: } jaroslav@8: public static double magicOne() { jaroslav@8: Instance i = new Instance(10, 3.3d); jaroslav@10: i.b = (byte)0x09; jaroslav@137: return (i.in - i.b) * i.d; jaroslav@8: } jaroslav@14: public static int virtualBytes() { jaroslav@13: Instance i = new InstanceSub(7, 2.2d); jaroslav@12: i.setByte((byte)0x0a); jaroslav@14: Instance i2 = new Instance(3, 333.0d); jaroslav@14: i2.setByte((byte)44); jaroslav@14: return i.getByte() + i2.getByte(); jaroslav@12: } jaroslav@15: public static float interfaceBytes() { jaroslav@15: GetByte i = new InstanceSub(7, 2.2d); jaroslav@15: return i.getByte(); jaroslav@15: } jaroslav@639: public static boolean instanceOf(int sub) { jaroslav@16: Instance i = createInstance(sub); jaroslav@16: return isInstanceSubOf(i); jaroslav@16: } jaroslav@30: public static int castsWork(boolean interfc) { jaroslav@639: Instance i = createInstance(2); jaroslav@30: if (interfc) { jaroslav@30: GetByte b = (GetByte)i; jaroslav@30: } else { jaroslav@30: InstanceSub s = (InstanceSub)i; jaroslav@30: } jaroslav@30: return 5; jaroslav@30: } jaroslav@30: jaroslav@16: private static boolean isInstanceSubOf(Instance instance) { jaroslav@16: return instance instanceof InstanceSub; jaroslav@16: } jaroslav@639: private static Instance createInstance(int type) { jaroslav@639: switch (type) { jaroslav@639: case 0: return null; jaroslav@639: case 1: return new Instance(); jaroslav@639: case 2: return new InstanceSub(3, 0); jaroslav@639: } jaroslav@639: throw new IllegalArgumentException(); jaroslav@16: } jaroslav@16: private static boolean isNull() { jaroslav@639: return createInstance(2) == null; jaroslav@16: } jaroslav@226: jaroslav@226: @JavaScriptBody(args = "obj", body = "return obj.constructor;") jaroslav@226: static Object constructor(Object obj) { jaroslav@226: return obj; jaroslav@226: } jaroslav@226: jaroslav@226: public static boolean sharedConstructor() { jaroslav@226: class X { jaroslav@226: } jaroslav@226: jaroslav@226: X x1 = new X(); jaroslav@226: X x2 = new X(); jaroslav@226: jaroslav@226: return constructor(x1) == constructor(x2); jaroslav@226: } jaroslav@226: public static boolean differentConstructor() { jaroslav@226: class X { jaroslav@226: } jaroslav@226: class Y { jaroslav@226: } jaroslav@226: jaroslav@226: X x = new X(); jaroslav@226: Y y = new Y(); jaroslav@226: jaroslav@226: return constructor(x) == constructor(y); jaroslav@226: } jaroslav@239: @JavaScriptBody(args = {}, body = "return {};") jaroslav@239: private static Object jsObj() { jaroslav@239: return null; jaroslav@239: } jaroslav@239: jaroslav@239: public static boolean iofObject() { jaroslav@239: return jsObj() instanceof Object; jaroslav@239: } jaroslav@442: jaroslav@442: public static int jscall() { jaroslav@442: return jsgetbytes(new Instance()); jaroslav@442: } jaroslav@442: jaroslav@1632: public static int noInstOfExposed() { jaroslav@1632: return countInstOf(10); jaroslav@1632: } jaroslav@1632: jaroslav@1632: @JavaScriptBody(args = "o", body = "var i = 0; for (p in o) { if (p.toString().indexOf('instOf') >= 0) i++; } return i;") jaroslav@1632: private static native int countInstOf(Object o); jaroslav@1632: jaroslav@442: @JavaScriptBody(args = { "instance" }, body = "return instance.getByte__B();") jaroslav@442: private static native int jsgetbytes(Instance instance); jaroslav@1467: jaroslav@1467: int sum(int i, int i0) { jaroslav@1467: return i + i0; jaroslav@1467: } jaroslav@8: }